ID生成策略----xml配置

id生成策略

1.对应项目:hibernate_0400_ID

2.注意:

    a)我们观察hibernate生成表的结构并不是为了将来就用它生成,(可能还有自己的扩展,比如index等)

而是为了明白我们应该建立什么样的表和实体类映射。

 

 

3.

 

 

 

id主键:

1)在mysql用自增字段,用auto increatment

   在oracle  用 sequence

注意:

       对于类里面的对象里的这个值就不可以指定它了。得靠程序(数据库)帮我自动生成;

 

hibernate或JPA已实现这样的能力,就是通过设置-->告诉它id怎么生成,这样的话,你写程序的时候就不用设这个id了。

 

----id的生成策略。

 

 

  

    

测试类:

使用junit进行

约定俗成的  在类的后面加Test是测试类HibernateIDTest

在方法的前面加Test是测试方法

 

 

 

案例:

1.查看文档自动生成id的

看文档的习惯是,先找目录,找不到再进行搜索。

 

对象/关系数据库映射基础(Basic O/R Mapping)有一个id

  <generator class="generatorClass"/> 

可的<generator>子元素是一个Java类的名字, 用来为该持久化类的实例生成唯一的标识。

 

uuid  university  Unicode id  全球唯一的id-----type string

native  会根据数据库为oracle或是mysql进行使用sequence 或是auto_increment

 

 

设置了generator,在测试类中就不需要再进行设置了。

 

对于xml配置文件进行生成uuid

 

生成的sql

id varchar(255) not nul

 

 

代码案例:

/hibernate_0400_ID/src/com/zhuhw/hibernate/model/Student.hbm.xml

 

Java代码  收藏代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC  
  3.         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <!-- 找不到entity,是因为这个类没改包名 -->  
  6. <hibernate-mapping package="com.zhuhw.hibernate.model">  
  7. <class name="Student">  
  8. <!-- id主键;name=id对应的是Student中的getid() -->  
  9. <id name="id"  >  
  10. <generator class="uuid"></generator>  
  11. </id>  
  12. <property name="name"/>  
  13. <property name="age" />  
  14. <!-- hibernater知道了怎么将class与表中的字段对应到一起了 -->  
  15. </class>  
  16. </hibernate-mapping>  

 

 

 

 

/hibernate_0400_ID/src/com/zhuhw/hibernate/model/Student.java

 

Java代码  收藏代码
  1. package com.zhuhw.hibernate.model;  
  2.    
  3. public class Student {  
  4. /*private int id; 
  5. public int getId() { 
  6. return id; 
  7. public void setId(int id) { 
  8. this.id = id; 
  9. }*/  
  10. private String id;  
  11.    
  12. public String getId() {  
  13. return id;  
  14. }  
  15. public void setId(String id) {  
  16. this.id = id;  
  17. }  
  18. public String getName() {  
  19. return name;  
  20. }  
  21. public void setName(String name) {  
  22. this.name = name;  
  23. }  
  24. public int getAge() {  
  25. return age;  
  26. }  
  27. public void setAge(int age) {  
  28. this.age = age;  
  29. }  
  30. private String name;  
  31. private int age;  
  32. }  

 

 

 

/hibernate_0400_ID/src/hibernate.cfg.xml

 

Java代码  收藏代码
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <!DOCTYPE hibernate-configuration PUBLIC  
  3.         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  
  4.         "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">  
  5.    
  6. <hibernate-configuration>  
  7.    
  8.     <session-factory>  
  9.    
  10.         <!-- Database connection settings -->  
  11.         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>  
  12.         <property name="connection.url">jdbc:mysql://localhost/hibernate</property>  
  13.         <property name="connection.username">root</property>  
  14.         <property name="connection.password">root</property>  
  15.    
  16.         <!-- JDBC connection pool (use the built-in) -->  
  17.         <!--<property name="connection.pool_size">1</property>-->  
  18.    
  19.         <!-- SQL dialect -->  
  20.         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>  
  21.    
  22.         <!-- Enable Hibernate's automatic session context management -->  
  23.         <!--<property name="current_session_context_class">thread</property>-->  
  24.    
  25.         <!-- Disable the second-level cache  -->  
  26.         <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>  
  27.    
  28.         <!-- Echo all executed SQL to stdout -->  
  29.         <property name="show_sql">true</property>  
  30.         <property name="format_sql">true</property>  
  31.    
  32.         <!-- Drop and re-create the database schema on startup -->  
  33.         <property name="hbm2ddl.auto">update</property>  
  34.         
  35.         <mapping resource="com/zhuhw/hibernate/model/Student.hbm.xml"/>  
  36.    
  37.    <mapping class="com.zhuhw.hibernate.model.Teacher"/>    
  38.     </session-factory>  
  39.    
  40. </hibernate-configuration>  

 

 

 

/hibernate_0400_ID/test/com/zhuhw/hibernate/model/HibernateIDTest.java

 

Java代码  收藏代码
  1. package com.zhuhw.hibernate.model;  
  2.    
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.cfg.AnnotationConfiguration;  
  6. import org.junit.AfterClass;  
  7. import org.junit.BeforeClass;  
  8. import org.junit.Test;  
  9.    
  10. public class HibernateIDTest {  
  11.    
  12. public static SessionFactory sf = null;  
  13. @BeforeClass  
  14. public  static void beforeClass(){  
  15. sf = new AnnotationConfiguration().configure().buildSessionFactory();  
  16. }  
  17.    
  18. @Test  
  19. public void TestID(){  
  20. Student s = new Student();  
  21. /*配置文件中使用generator 
  22. * s.setId(9); 
  23. * */  
  24. s.setName("yuzhou1");  
  25. s.setAge(1);  
  26.    
  27.    
  28. Session session = sf.openSession();  
  29.    
  30. session.beginTransaction();  
  31. session.save(s);  
  32. session.getTransaction().commit();  
  33. session.close();  
  34. }  
  35.    
  36. @AfterClass  
  37. public static void afterClass(){  
  38. sf.close();  
  39. }  
  40. }  
  41.    

 

 

运行结果:

id varchar(255) 

将id生成的是String进行存储的。

 

先将student表drop掉

 

使用native

配置文件

<id name="id"  >

<generator class="native"></generator>

</id>

 

在java 类中

将主键设置为int类型即可。

 

运行结果:

create table Student (

  id integer not null auto_increment,  

  varchar(255), age integer, primary key (id))

 

posted @ 2016-01-08 09:54  烟-波-天-客  阅读(441)  评论(0编辑  收藏  举报