标签:
1.increment
适用于代理主键。由hibernate自动以递增的方式生成表识符,每次增量为1。
mysql
Hibernate: select max(id) from users
Hibernate: insert into hjd.users (name, pass, sex, id) values (?, ?, ?, ?)
适用于所有的数据库。
2.identity
适用于代理主键。由底层数据库生成表识符。条件是数据库支持自动增长数据类型。
Hibernate: insert into hjd.users (name, pass, sex) values (?, ?, ?)
适用于mysql DB2 SQLServer Sybase等
sequence 适用于代理主键。Hibernate根据底层数据库序列生成标识符。条件是数据库支持序列。
DB2 Oracle 等
3.hilo
适用于代理主键。Hibernate根据hign/low算法生成标识符。Hibernate把特定表的字段作为“hign”值。默认情况下,采用hibernate_unique_key表的next_hi字段。
<generator class="hilo">
<param name="table">hilo_ids</param>
<param name="column">value</param>
<param name="max_lo">100</param>
</generator>
Hibernate: create table hilo_ids ( value integer )
Hibernate: insert into hilo_ids values ( 0 )
四月 24, 2015 2:37:56 下午 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000230: Schema export complete
Hibernate: select value from hilo_ids for update
Hibernate: update hilo_ids set value = ? where value = ?
Hibernate: insert into hjd.users (name, pass, sex, id) values (?, ?, ?, ?)
4.native
适用于代理主键。根据底层数据库对自动生成表示符的能力来选择identity、sequence、hilo
5.uuid.hex
适用于代理主键。Hibernate采用128位的UUID算法来生成标识符。该算法能够在网络环境中生成唯一的字符串标识符,这种策略并不流行,因为字符串类型的主键比整数类型的主键占用更多的数据库空间。
6.assigned
适用于自然主键。由java程序负责生成标识符。不能把setID()方法声明为private的。尽量避免使用自然主键。
public class Dept implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private Date cdate;
public Dept() {
super();
// TODO Auto-generated constructor stub
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getCdate() {
return cdate;
}
public void setCdate(Date cdate) {
this.cdate = cdate;
}
}
<class name="Dept" table="depts" catalog="hjd">
<!-- id主键 -->
<id name="name" column="name" type="string">
<generator class="assigned"/>
</id>
<!-- class属性 -->
<property name="cdate" column="cdate" type="date"/>
</class>
7.复合主键:
第一种写法
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
private String sex;
public Customer() {
super();
// TODO Auto-generated constructor stub
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
<hibernate-mapping package="hiber01.domain">
<class name="Customer" table="customers" catalog="hjd">
<composite-id>
<key-property name="firstName" length="50" type="string"/>
<key-property name="lastName" length="50" type="string"/>
</composite-id>
<property name="sex" length="4"></property>
</class>
</hibernate-mapping>
@Test
public void save() {
// 1.获取Configuration对象
Configuration configuration = new Configuration().configure();
// 2.获取SessionFactory对象
SessionFactory sessionFactory = configuration.buildSessionFactory();
// 3.获取session,打开事务
Session session = sessionFactory.openSession();
Transaction ts = session.beginTransaction();
Customer customer=new Customer();
customer.setFirstName("zz");
customer.setLastName("sf");
customer.setSex("女");
try {
session.save(customer);
} catch (Exception e) {
ts.rollback();
e.printStackTrace();
}
ts.commit();
session.close();
}
第二种写法:
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
private CustomerId customerId;
private String sex;
public Customer() {
super();
// TODO Auto-generated constructor stub
}
public CustomerId getCustomerId() {
return customerId;
}
public void setCustomerId(CustomerId customerId) {
this.customerId = customerId;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
public class CustomerId implements Serializable{
private static final long serialVersionUID = 1L;
private String firstName;
private String lastName;
public CustomerId() {
super();
// TODO Auto-generated constructor stub
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
<class name="Customer" table="customers" catalog="hjd">
<composite-id name="customerId" class="CustomerId">
<key-property name="firstName" length="50" type="string"/>
<key-property name="lastName" length="50" type="string"/>
</composite-id>
<property name="sex" length="4"></property>
</class>
注意:两个类都需要实现可序列化接口,否则会报错
标签:
原文地址:http://www.cnblogs.com/ly-radiata/p/4454220.html