Hibernateday05主键生成器(hilo)
主键生成器 generator class="xxx"
1.increment 先查询数据表的主键的最大值,然后加1
在实际开发过程中不会使用increment:不能解决并发问题
2.foreign 只能在共享主键1:1中使用
3.* sequence【序列】适用的数据库Oracle 、DB2、Postgre
默认调用hibernate_sequence
使用自定义的sequence控制主键的增长
1.创建序列
create sequence 序列名称 increment by 步进 start with 起始值;
2.在映射文件中使用
<generator class="sequence">
<param name="sequence">g_user_seq</param>
</generator>
4.identity 使用的数据库 Sqlserver Mysql
特点:在创建表时要为主键设定自动生长的关键词auto_increment
create table g_user(
id int auto_increment primary key
);
5.* native 特点:根据方言进行数据库的判断,根据数据库的类型
自动选择sequence和indentity
<generator class="native"></generator>
6.assigned 特点:可以让程序员手动指定主键的值(公司定义的算法),在实际开发中使用
较多
<generator class="assigned"></generator>
在保存对象时应该手动指定主键值
7.uuid 特点:uuid长度比较长,占空间,跨数据库,移植性比较好
8.* hilo 高低位算法:在hibernate 中最常用的一种主键生成方式,通过一张表来维护hi的
值(hi的值必须初始化)
1.创建维护hi值的表
create table hibernate_hilo(next_hi integer);next_hi维护高位值
insert into hibernate_hilo values (0);
2.在映射文件中配置
<generator class="hilo">
<!-- 指明维护高位值的表 -->
<param name="table">hibernate_hilo</param>
<!-- 指明维护高位的列名 -->
<param name="column">next_hi</param>
<!-- 设置低位的最大值 -->
<param name="max_lo">100</param>
执行的原理:
1.获得hi的值,并且将hi的值加1保存
2.获得lo的值,从0到max_lo中循环取值,每次值的差为1
3.根据公式:hi*(max_lo+1)计算生成主键的值
注意:到hi为0时,O*(max_lo+1);时lo会跳过0从1开始
特点:跨数据库,维护性比较高
9:seqhilo :将hilo中的数据表换成了sequence
1.O 创建对象在com.jsu.hb.pojo包中User.java
package com.jsu.hb.pojo;
import java.util.Date;
public class User {
private Integer id;
private String name;
private Date birthday;
private int age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
2.R建表
create table t_user(
t_id integer primary key,
t_name varchar2(20),
t_birthday date,
t_age integer
)
select * from t_user
create table hibernate_hilo(next_hi integer);
insert into hibernate_hilo values(0);
commit;
select * from t_user
select * from hibernate_hilo
3.M.提供映射文件在User.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>
<!-- name:所操作对象的全路径 table:该对象所对应的表名 -->
<class name="com.jsu.hb.pojo.User" table="t_user">
<!-- 主键配置
name:对象当中的主键名 User实体类当中
column:与表对应的列名
type:java当中的数据类型
-->
<id name="id" column="t_id" type="java.lang.Integer">
<!-- 定义主键生成器 -->
<generator class="hilo">
<param name="table">hibernate_hilo</param>
<param name="column">next_hi</param>
<param name="max_lo">10</param>
</generator>
</id>
<!-- 普通属性的配置,非主键属性的配置 -->
<property name="name" column="t_name" type="java.lang.String"></property>
<property name="birthday" column="t_birthday" type="java.util.Date"></property>
<property name="age" column="t_age" type="java.lang.Integer"></property>
</class>
</hibernate-mapping>
4.在hibernate.cfg.xml文件中对User.hbm.xml文件进行注册
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- show_sql:是否显示hibernate执行的SQL语句,默认为false -->
<property name="show_sql">true</property>
<!-- show_sql:是否显示hibernate执行格式化输出的SQL语句,默认为false -->
<property name="format_sql">true</property>
<!-- 配置与数据库连接的参数 -->
<property name="connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:oracle</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<!-- 2.自身属性相关的配置
dialect:方言
hibernate根据dialect的配置进行特定数据性能方面的调优
-->
<property name="dialect">org.hibernate.dialect.Oracle9iDialect</property>
<mapping resource="com/jsu/hb/pojo/User.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>
5提供工具方法HibernateUtil.java
package com.jsu.hb.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static SessionFactory sf;
private static ThreadLocal<Session> tl= new ThreadLocal<Session>();
static{
try{
Configuration cfg = new Configuration();
cfg.configure();
sf=cfg.buildSessionFactory();
}catch(Exception e){
e.printStackTrace();
}
}
public static Session openSession(){
return sf.openSession();
}
public static Session getCurrentSession(){
Session session = tl.get();//先从储存的线程中查找
if(session==null){
session=openSession();
tl.set(session);
return session;
}
return session;
}
}
6.提供测试类TestID.java
package com.jsu.hb.test;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;
import com.jsu.hb.pojo.User;
import com.jsu.hb.util.HibernateUtil;
public class TestID {
@Test
public void save(){
User u = new User();
u.setAge(23);
u.setName("leon");
u.setBirthday(new Date());
Session session = HibernateUtil.getCurrentSession();
Transaction tx = session.getTransaction();
tx.begin();
session.save(u);
tx.commit();
}
}