一、 第一个Hibernate小程序
搭建一个可以存储事件(Event)的小程序
1. 实现我们的第一个类Event,一个标准的JavaBean
package com.iteye.hibernate.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; } }
2. 创建我们的mapping文件
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.iteye.hibernate.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"/> </class> </hibernate-mapping>
3. 进行hibernate的配置
现在你已经有了一个java类和一个Event的映射文件,接下来来配置hibernate的配置文件。在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">oracle.jdbc.driver.OracleDriver</property> <property name="connection.url">jdbc:oracle:thin:@urhost:1521:urdb</property> <property name="connection.username">urname</property> <property name="connection.password">urpwd</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">1</property> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.OracleDialect</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">update</property> <mapping resource="com/iteye/hibernate/domain/Event.hbm.xml"/> </session-factory> </hibernate-configuration>
上面的文件中配置了Hibernate的SessionFactory。SessionFactory是一个数据库的全局工厂,用于和数据库的连接。如果你有多个数据了,那你可以在不同的hibernate配置文件中配置多个SessionFactory
4. 配置必须的jar包
把hibernate-distribution-3.6.8.Final\lib\required目录下的所有jar包,\hibernate-distribution-3.6.8.Final\目录下的hibernate3.jar以及\hibernate-distribution-3.6.8.Final\lib\jpa 下的hibernate-jpa-2.0-api-1.0.1.Final.jar
和oracle的JDBC驱动加入到项目的classpath中
5. 创建一个帮助类来帮助获取SessionFactory实例
HibernateUtil.java
package com.iteye.hibernate.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { return new Configuration().configure().buildSessionFactory(); } public static SessionFactory getSessionFactory() { return sessionFactory; } } 6. 创建测试类来运行我们的程序 EventManager.java package com.iteye.hibernate; import java.util.Date; import org.hibernate.Session; import com.iteye.hibernate.domain.Event; import com.iteye.hibernate.util.HibernateUtil; public class EventManager { public static void main(String[] args) { EventManager mgr = new EventManager(); mgr.createAndStoreEvent("My Event", new Date()); HibernateUtil.getSessionFactory().close(); } 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(); } }
然后运行此类,至此我们的第一个Hibernate小程序运行成功
你会在Console看到如下sql语句
Hibernate: select hibernate_sequence.nextval from dual Hibernate: insert into EVENTS (EVENT_DATE, title, EVENT_ID) values (?, ?, ?)
说明数据已经插入数据库