我从网上下载了 hibernate-release-4.3.0.Final.zip,解压缩,把/lib/required文件夹下的所有jar包加入到eclipse项目中的Referenced Libraries里面。然后,我粗略地阅读了documentation/manual/en-US/html/下的文档,按部就班地做了如下的最简单实例(添加了一些修改)。由于文档中用的是HSQLDB,我就从网上下载了hsqldb-2.3.1.zip,解压缩,把/lib下的两个Jar包同样加入项目。在HSQLDB的/bin目录下,运行runServer.bat以启动数据库,再运行runManagerSwing.bat以启动管理数据库的简单GUI工具(选择HSQL Database Engine Server)。项目所要用到的一个表格的创建SQL为:
CREATE TABLE EVENTS ( EVENT_ID INT IDENTITY, EVENT_DATE DATE, EVENT_TITLE VARCHAR(100) )
Eclipse项目的根目录就是自带的src目录,在其下新建一个hibernate.cfg.xml。其内容为:
<?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/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.internal.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">update</property> <mapping resource="com/example/domain/Event.hbm.xml"/> </session-factory> </hibernate-configuration>
以上代码实测可行,不用修改。
模型类都放在了com.example.domain目录下。这个例子中只有一个类,为Event:
package com.example.domain; 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; } }
用行话说,就是一个普通JavaBean,一个POJO。
还要放在com.example.domain目录中的、与Event.java靠在一起的是它的ORM配置文件,Event.hbm.xml:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.example.domain"> <class name="Event" table="EVENTS"> <id name="id" column="EVENT_ID"> <generator class="native" /> </id> <property name="date" type="timestamp" column="EVENT_DATE"/> <property name="title" type="string" column="EVENT_TITLE"/> </class> </hibernate-mapping>
映射比较明了,有关generator的详细信息这里不提及。
根据官方文档里面给出的示例,我也建立了一个Hibernate助手类:HibernateUtil,放在com.example.util包下:
package com.example.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml return 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; } }
这个类我也是照抄的,用于提供SessionFactory对象。不过其中的buildSessionFactory显示为Depricated,有待进一步解决。
最后一个类,即有main函数的EventManager.java,放在com.example包下:
package com.example; import java.util.Date; import org.hibernate.Session; import com.example.domain.Event; import com.example.util.HibernateUtil; public class EventManager { 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(); } public static void main(String[] args) { EventManager mgr = new EventManager(); mgr.createAndStoreEvent("My Event", new Date()); HibernateUtil.getSessionFactory().close(); } }
运行EventManager,在HSQLDB的Swing GUI工具里面运行Select * from EVENTS,就可以看到刚刚插入的一行数据了。
结果为:
1月5日也是我的生日哦!对的,今天我过生日。(^_^)