7.6 Hibernate:内置生成器 – increment
来源:互联网 发布:车主名录软件 编辑:程序博客网 时间:2024/05/21 09:10
由 Hibernate 管理主键,自动以递增的方式生成标识符,每次增量为 1
。
在每次插入数据前从数据库中取出主键当前最大值(每个 session 只取一次),以该值为基础加 1
在内存中生成新的主键,该操作不依赖于底层数据库,所以可跨数据库。
Hibernate 调用 org.hibernate.id.IncrementGenerator
类里面的 generate()
方法,使用 select max(idColumnName) from tableName
语句获取主键最大值。该方法被声明成了 synchronized
,所以在一个独立的 Java 虚拟机内部是没有问题的。然而,在多个 JVM 同时并发访问数据库 select max
时就可能取出相同的值,再 insert
就会发生 Dumplicate entry
的错误。所以只能有一个 Hibernate 应用进程访问数据库,否则就可能产生主键冲突,所以不适合多进程并发更新数据库,适合单一进程访问数据库,不能用于群集环境。
官方文档:只有在没有其他进程往同一张表中插入数据时才能使用,在集群下不要使用。
特点:跨数据库,不适合多进程并发更新数据库,适合单一进程访问数据库,不能用于集群环境。
使用 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="increment" /> </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 自动生成的表结构定义如下:
数据库中新插入两条记录,主键从 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 = "increment") 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.6 Hibernate:内置生成器 – increment
- 7.4 Hibernate:内置生成器 – hilo
- 7.5 Hibernate:内置生成器 – identity
- 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内置的持久化标识符生成器
- 史上最全的SpringMVC学习笔记
- linux学习-1
- 【简记】Operating System——Dead Lock
- Android TV开发 焦点框
- Shell脚本编写进度条代码
- 7.6 Hibernate:内置生成器 – increment
- QString 常用函数
- 【Linux基础】Linux目录结构
- Linux的入门级常用操作命令
- 51Nod-1476-括号序列的最小代价
- 欧拉函数(hdu2824)
- springMVC的执行流程
- 51nod1016 水仙花数 V2
- cdh安装配置笔记