主键生成器

assined 主键的值 程序指定

uuid  hibernate帮你生成uuid,所以主键必须为String

Identity 数据库表必须支持自动增长,新的主键的产生是由数据库完成的,并不是由hibernate或者程序员完成的

increment 递增加1

1         <id name="pid" column="pid" length="200" type="java.lang.Long">
2             <!-- 
3                 主键的产生器
4                   就该告诉hibernate容器用什么样的方式产生主键
5              -->
6             <generator class="increment"></generator>
7         </id>
  1 package cn.itcast.hibernate.sh.generator;
  2 
  3 import org.hibernate.Session;
  4 import org.hibernate.Transaction;
  5 import org.junit.Test;
  6 
  7 import cn.itcast.hibernate.sh.domain.Person;
  8 import cn.itcast.hibernate.sh.utils.HiberanteUtils;
  9 
 10 public class GeneratorTest extends HiberanteUtils{
 11     /**
 12      *     Hibernate: select max(pid) from Person
 13         Hibernate: insert into Person (pname, psex, pid) values (?, ?, ?)
 14         
 15         说明:
 16            1、主键的类型必须是数字
 17            2、主键的生成是由hibernate内部完成的,程序员不需要干预
 18            3、这种生成机制效率比较低
 19      */
 20     @Test
 21     public void testIncrement(){
 22         Session session = sessionFactory.openSession();
 23         Transaction transaction = session.beginTransaction();
 24         
 25         Person person = new Person();
 26         person.setPname("上海第一期班长");
 27         person.setPsex("女");
 28         
 29         /**
 30          * 参数必须持久化对象
 31          */
 32         session.save(person);
 33         
 34         transaction.commit();
 35         session.close();
 36     }
 37     
 38     /*
 39      * Hibernate: insert into Person (pname, psex) values (?, ?)
 40      * 说明
 41      *   1、新的主键的产生是由数据库完成的,并不是由hibernate或者程序员完成的
 42      *   2、该表必须支持自动增长机制
 43      *   3、效率比较高
 44      */
 45     @Test
 46     public void testIdentity(){
 47         Session session = sessionFactory.openSession();
 48         Transaction transaction = session.beginTransaction();
 49         
 50         Person person = new Person();
 51         person.setPname("上海第一期班长");
 52         person.setPsex("女");
 53         
 54         /**
 55          * 参数必须持久化对象
 56          */
 57         session.save(person);
 58         
 59         transaction.commit();
 60         session.close();
 61     }
 62     
 63     /**
 64      * 由程序设置主键
 65      */
 66     @Test
 67     public void testAssigned(){
 68         Session session = sessionFactory.openSession();
 69         Transaction transaction = session.beginTransaction();
 70         
 71         Person person = new Person();
 72         person.setPname("上海第一期班长");
 73         person.setPsex("女");
 74         
 75         /**
 76          * 参数必须持久化对象
 77          */
 78         session.save(person);
 79         
 80         transaction.commit();
 81         session.close();
 82     }
 83     
 84     /**
 85      * 1、UUID是由hibernate内部生成的
 86      * 2、主键的类型必须是字符串String类型
 87      */
 88     @Test
 89     public void testUUID(){
 90         Session session = sessionFactory.openSession();
 91         Transaction transaction = session.beginTransaction();
 92         
 93         Person person = new Person();
 94         person.setPname("上海第一期班长");
 95         person.setPsex("女");
 96         
 97         /**
 98          * 参数必须持久化对象
 99          */
100         session.save(person);
101         
102         transaction.commit();
103         session.close();
104     }
105     
106     
107     /**
108      * hibernate内部是根据主键生成器来生成主键的,在客户端设置主键不一定起作用,在assigned的时候起作用
109      */
110     @Test
111     public void testIdentity_ID(){
112         Session session = sessionFactory.openSession();
113         Transaction transaction = session.beginTransaction();
114         Person person = new Person();
115         person.setPid(1L);
116         person.setPname("aa");
117         person.setPsex("aa");
118         session.save(person);
119         transaction.commit();
120         session.close();
121     }
122 }
原文地址:https://www.cnblogs.com/friends-wf/p/3772498.html