HIBERNATE是一個數據庫層持久框架,今天下載了一個3.3.1版試用了一下;
首先創建一個對象類:
import java.util.Date;
public class Event {
private Long id;
private String title;
private Date date;
public Event() {
}
public Long getId() {
return id;
}
private void setId(Long id) {
this.id = id;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
接下來創建相應對象的XML配置文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="events.Event" table="EVENTS">
<id name="id" column="EVENT_ID">
<generator class="native" />
</id>
<property name="date" type="timestamp" column="EVENT_DATE" />
<property name="title" />
</class>
</hibernate-mapping>
接下來就是創建Hibernate的配置文件hibernate.cfg.xml:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:hsql://localhost</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!--
Drop and re-create the database schema on startup <property
name="hbm2ddl.auto">create</property>
-->
<mapping resource="events/Event.hbm.xml" />
</session-factory>
</hibernate-configuration>
這裡對於數據則采用了純JAVA實現的HSQLDB。
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
// 此處還可以在configure方法中指定具體的配置文件 configure(File configFile)
sessionFactory = new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
store:開始往數據庫中寫入數據
list:讀取目前的記錄並在控制台打印出來
public class EventManager {
public static void main(String[] args) {
EventManager mgr = new EventManager();
if (args[0].equals("store")) {
mgr.createAndStoreEvent("My Event", new Date());
} else if (args[0].equals("list")) {
List events = mgr.listEvents();
for (int i = 0; i < events.size(); i++) {
Event theEvent = (Event) events.get(i);
System.out.println("Event: " + theEvent.getTitle() + " Time: " + theEvent.getDate());
}
}
HibernateUtil.getSessionFactory().close();
}
private List listEvents() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Event").list();
session.getTransaction().commit();
return result;
}
private void createAndStoreEvent(String title, Date theDate) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Event theEvent = new Event();
theEvent.setTitle(title);
theEvent.setDate(theDate);
session.save(theEvent);
session.getTransaction().commit();
}
}
最終的SRC目錄結構如下:
src
│ hibernate.cfg.xml
├─events
│ Event.hbm.xml
│ Event.java
└─util
EventManager.java
HibernateUtil.java
引用的LIB包有:
lib
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate3.jar
hsqldb.jar
javassist-3.4.GA.jar
jta-1.1.jar
slf4j-api-1.5.2.jar
slf4j-simple-1.5.2.jar
當然還需要啟動數據庫:
F:\dev\hsqldb\mytest>java -classpath ../lib/hsqldb.jar org.hsqldb.Server
F:\dev\hsqldb\mytest>java -cp ../lib/hsqldb.jar org.hsqldb.util.DatabaseManager
Hibernate的JAVADOC文檔,沒找到下載的鏈接,只有一個在線鏈接:http://www.hibernate.org/hib_docs/v3/api/