准备工作
创建表
CREATE TABLE `events` (
`event_id` int(11) NOT NULL auto_increment,
`title` varchar(200) default NULL,
`event_date` timestamp ,
PRIMARY KEY (`event_id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
POJO类
public class Event {
private Long id;
private String title;
private Date date;
public Event(){}
public Event(Long id, String title, Date date){
this.id = id;
this.title = title;
this.date = date;
}
//get set method
}
NOTICE:对POJO类,hibernate要求一定要定义一个无参数的构造函数,就像本例子的public Event(){}
<?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">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost/hibernate</property>
<property name="connection.username"></property>
<property name="connection.password"></property>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect depending on deploying database, I use MySQL here. -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</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/lam/hibernate/model/Event.hbm.xml"/>
</session-factory>
</hibernate-configuration>
genertator-increment
如mysql,主键自增长
Event.hbm.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 package="com.lam.hibernate.model">
<class name="Event" table="events">
<id name="id" column="event_id">
<generator class="increment" />
</id>
<property name="date" type="timestamp" column="event_date" />
<property name="title" />
</class>
</hibernate-mapping>
测试
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Long event_id = (Long) session.save(new Event(null, "news of east", new Date()));
//notice:session close automatically when commit method invoked.
session.getTransaction().commit();
System.out.println("event_id: " + event_id);
genertator-uuid
使用一个128位的UUID算法生成一个唯一的字符串类型标识,生成的算法使用IP地址。UUID使用32位的十六进制数字编码成为为字符串的。
表:
create table article(
article_id varchar(46),
title varchar(200),
article_date timestamp,
primary key (article_id)
);
Article.hbm.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">
<!-- this is where the hibernate mapping files comes into play. -->
<hibernate-mapping package="org.lam.detector.hibernate.charpter1.model">
<class name="Article" table="article">
<!-- The id element is the declaration of the identifier property. -->
<id name="article_id">
<!-- class option:increment, uuid, guid -->
<generator class="uuid"></generator>
</id>
<property name="title"/>
<property name="create_date" type="timestamp"/>
</class>
</hibernate-mapping>
测试:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String article_id = (String) session.save(new Article(null, "content", new Date()));
System.out.println(article_id);
session.getTransaction().commit();
结果:402894e44e0f7d3c014e0f7d3f030001
genertator-guid
对于MS SQL Server,MySQL,hibernate使用数据库生成一个GUID字符串。
如MYSQL,使用了MYSQL提供的函数uuid()生成ID。
Article.hbm.xml:
<!-- class option:increment, uuid, guid -->
<generator class="guid"></generator>
XML的其他部分不变,generator元素的class值改为guid。
测试:
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
String article_id = (String) session.save(new Article(null, "content", new Date()));
System.out.println(article_id);
session.getTransaction().commit();
结果输出:
Hibernate: select uuid()
d820152b-170f-11e5-b81b-f203370e9deb
Hibernate: insert into article (title, create_date, article_id) values (?, ?, ?)
可以看出MYSQL使用uuid()生成ID。
genertator-assigned
在model类的XML文件中,类的主键的配置如果没有指定gererator元素时,assigned是默认的策略。
当generator配置的是assigned,或者没有配置generator,assigned是默认的主键生成策略,表的ID主键是由应用程序生成的。
Article.hbm.xml,生成策略改成:
<generator class="assigned"></generator>
测试:
String article_id = (String) session.save(new Article("d820152b-170f-11e5-b81b-f203370e9dcc", "content", new Date()));
System.out.println(article_id);
在这个测试例子中,Article的ID是由应用程序传值:d820152b-170f-11e5-b81b-f203370e9dcc
genertator-hilo
使用高/低算法来高效地生成long,short,int类型的标识,其中通过一张表和字段(默认分别是 hibernate_unique_key和next_hi)作用高值的来源。只指定同一个数据库的情况下,高/低算法生成的标识才是唯一的。
其中POJO类的配置:
<class name="HiloFoo" table="hilo_foo">
<!-- The id element is the declaration of the identifier property. -->
<id name="id" column="hilo_id">
<!-- class option:increment, uuid, guid, assigned, hilo -->
<generator class="hilo">
<param name="table">hilo_value</param>
<param name="column">next_value</param>
<param name="max_lo">0</param>
</generator>
</id>
<property name="date" type="timestamp" column="create_date"/>
</class>
由于配置指定了表和字段分别是hilo_value和next_value,所以我们首先要定义好表和字段:
Notice:表至少要有一条记录,否则hibernate执行save()时候会报错。
如果想查看复合主键的用法,点击打开链接