7.5 Hibernate:内置生成器 – identity
来源:互联网 发布:jive论坛源码 编辑:程序博客网 时间:2024/06/06 14:19
由数据库自己生成标识符,映射的主键必须设置为自增长,要求数据库底层支持自增长字段类型。
适用于 Sybase,MySQL,MS SQL Server,DB2 和 Hypersonic SQL,不适用 Oracle 因其无自增字段类型。
返回的 ID
类型为 short
,int
或 long
。
如果使用 MySQL 数据库,则主键字段必须设置为 auto_increment
。
特点:只能用在支持自动增长的字段数据库中使用。
使用 MySQL 演示:
1 使用 XML
1.1 持久化类定义:
package hibernate;import java.util.Date;public class Person { private Integer id; private String account; private String name; private Date birth; public Person() {} public Person(String account, String name, Date birth) { this.account = account; this.name = name; this.birth = birth; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "Person [id=" + id + ", account=" + account + ", name=" + name + ", birth=" + birth + "]"; }}
1.2 定义映射:
<?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="hibernate.Person" table="PERSON"> <id name="id" type="int"> <column name="ID" /> <generator class="identity" /> </id> <property name="account" type="java.lang.String"> <column name="ACCOUNT" /> </property> <property name="name" type="java.lang.String"> <column name="NAME" /> </property> <property name="birth" type="java.util.Date"> <column name="BIRTH" /> </property> </class></hibernate-mapping>
1.3 单元测试:
package hibernate;import java.util.Date;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;public class HibernateTest { @Test public void test() { Configuration configuration = new Configuration().configure("hibernate.cfg.xml"); SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); Transaction transaction = session.beginTransaction(); Date date = new Date(System.currentTimeMillis()); Person person1 = new Person("admin1", "Nick", date); session.save(person1); Person person2 = new Person("admin2", "King", date); session.save(person2); transaction.commit(); session.close(); sessionFactory.close(); }}
单元测试通过,查询数据库,Hibernate 自动生成的表结构定义如下:
可见主键 ID
为自增列。数据库中新插入两条记录,主键从 1
开始,每次递增 1
。
2 使用注解(annotation)
使用注解定义持久化类:
package hibernate;import java.util.Date;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.Table;import org.hibernate.annotations.GenericGenerator;@Entity@Tablepublic class Person { @Id @GeneratedValue(generator = "assignedGenerator") @GenericGenerator(name = "assignedGenerator", strategy = "identity") private Integer id; private String account; private String name; private Date birth; public Person() {} public Person(String account, String name, Date birth) { this.account = account; this.name = name; this.birth = birth; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } @Override public String toString() { return "Person [id=" + id + ", account=" + account + ", name=" + name + ", birth=" + birth + "]"; }}
运行【1.3 单元测试】,测试结果相同。
阅读全文
0 0
- 7.5 Hibernate:内置生成器 – identity
- 7.4 Hibernate:内置生成器 – hilo
- 7.6 Hibernate:内置生成器 – increment
- 7.7 Hibernate:内置生成器 – native
- 7.8 Hibernate:内置生成器 – select
- 7.9 Hibernate:内置生成器 – seqhilo
- 7.10 Hibernate:内置生成器 – sequence
- 7.11 Hibernate:内置生成器 – uuid
- hibernate内置生成器
- Hibernate内置标识符生成器
- Hibernate内置标识符生成器
- Hibernate内置标识符生成器
- 7.1 Hibernate:内置生成器 -- assigned
- 7.2 Hibernate:内置生成器 -- foreign
- 7.3 Hibernate:内置生成器 -- guid
- Hibernate提供的内置标识符生成器
- Hibernate内置对象标识符(OID)生成器
- Hibernate内置的持久化标识符生成器
- 洛谷 P1149 火柴棒等式
- 2017年年中反思
- 洛谷 P1351 联合权值
- MongoDB 安装和可视化工具
- Java中的设计模式
- 7.5 Hibernate:内置生成器 – identity
- Java用HttpsURLConnection访问https网站的时候如何跳过SSL证书的验证?
- Random取100以内的随机数
- 怎么转载 博客
- 解决MySql 数据库 提示:1045 access denied for user 'root'@'localhost' using password yes
- 16th 【并查集】亲属关系
- 贪心法-Fence Repair POJ
- iOS实时卡顿监控
- wireshark 抓包分析 TCPIP协议的握手