(三)映射对象标识符(OID)

所有项目导入对应的hibernate的jar包、mysql的jar包和添加每次都需要用到的HibernateUtil.java

第一节:Hibernate 用对象标识符(OID)来区分对象

 

 

 例子:

hibernate.cfg.xml

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7 
 8     <session-factory>
 9 
10         <!--数据库连接设置 -->
11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
13         <property name="connection.username">root</property>
14         <property name="connection.password">123456</property>
15 
16        
17         <!-- 方言 -->
18         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
19     
20         <!-- 控制台显示SQL -->
21         <property name="show_sql">true</property>
22 
23         <!-- 自动更新表结构 -->
24         <property name="hbm2ddl.auto">update</property>
25         
26           <mapping resource="com/wishwzp/model/Student.hbm.xml"/>
27 
28     </session-factory>
29 
30 </hibernate-configuration>

 

 Student.java

 1 package com.wishwzp.model;
 2 
 3 public class Student {
 4 
 5     private long id;
 6     private String name;
 7     
 8     public long getId() {
 9         return id;
10     }
11     public void setId(long id) {
12         this.id = id;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     @Override
21     public String toString() {
22         return "Student [id=" + id + ", name=" + name + "]";
23     }
24     
25     
26 }

 

Student.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Student" table="t_student">
 9         <id name="id" column="stuId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name"></property>
14     </class>
15 
16 </hibernate-mapping>

 

StudentTest.java

 1 package com.wishwzp.service;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 
 6 import com.wishwzp.model.Student;
 7 import com.wishwzp.util.HibernateUtil;
 8 
 9 public class StudentTest {
10     
11     public static void main(String[] args) {
12         SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
13         Session session=sessionFactory.openSession(); // 生成一个session
14         session.beginTransaction(); // 开启事务
15         
16         //获取主键为1的学生
17         Student s1=(Student)session.get(Student.class, Long.valueOf(1));//当执行到这里的时候Session缓冲中会有OID为1的Studnet对象
18         //获取主键为2的学生
19         Student s2=(Student)session.get(Student.class, Long.valueOf(2));//当执行到这里的时候Session缓冲中会有OID为2的Studnet对象
20         //获取主键为1的学生
21         Student s3=(Student)session.get(Student.class, Long.valueOf(1));//在Session缓冲中找到了UID为1,所以指向了UID为1的对象
22         System.out.println(s1==s2);//false
23         System.out.println(s1==s3);//true
24         session.getTransaction().commit(); // 提交事务
25         session.close(); // 关闭session
26     }
27 }

 

这是数据库的信息:

结果显示:

这就表明S1和S3是指向一个地址的 

 

第二节:Hibernate 对象标识符生成策略

 

主键的分类

业务主键VS 代理主键

代理主键是不具有业务性的;

1,increment 由Hibernate 自动以递增的方式生成标识符,适用代理主键;

2,identity 由底层数据库生成标识符;适用代理主键;

3,sequcence 由Hibernate 根据底层数据库的序列来生成标识符;适用代理主键;

4,hilo Hibernate 根据high/low 算法来生成标识符。适用代理主键

5,native 根据底层数据库对自动生成标识符的支持能力, 来选择identity,sequence 或hilo;适用代理主键;

 

increment 例子:

 hibernate.cfg.xml

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3         "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 
 6 <hibernate-configuration>
 7 
 8     <session-factory>
 9 
10         <!--数据库连接设置 -->
11         <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
12         <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
13         <property name="connection.username">root</property>
14         <property name="connection.password">123456</property>
15 
16        
17         <!-- 方言 -->
18         <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
19     
20         <!-- 控制台显示SQL -->
21         <property name="show_sql">true</property>
22 
23         <!-- 自动更新表结构 -->
24         <property name="hbm2ddl.auto">update</property>
25         
26           <mapping resource="com/wishwzp/model/Student.hbm.xml"/>
27 
28     </session-factory>
29 
30 </hibernate-configuration>

 

Student.java

 1 package com.wishwzp.model;
 2 
 3 public class Student {
 4 
 5     private long id;
 6     private String name;
 7     
 8     public long getId() {
 9         return id;
10     }
11     public void setId(long id) {
12         this.id = id;
13     }
14     public String getName() {
15         return name;
16     }
17     public void setName(String name) {
18         this.name = name;
19     }
20     @Override
21     public String toString() {
22         return "Student [id=" + id + ", name=" + name + "]";
23     }
24     
25     
26 }

 

Student.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Student" table="t_student">
 9         <id name="id" column="stuId">
10             <generator class="increment"></generator>
11         </id>
12         
13         <property name="name"></property>
14     </class>
15 
16 </hibernate-mapping>

 

StudentTest2.java

 1 package com.wishwzp.service;
 2 
 3 import org.hibernate.Session;
 4 import org.hibernate.SessionFactory;
 5 
 6 import com.wishwzp.model.Student;
 7 import com.wishwzp.util.HibernateUtil;
 8 
 9 public class StudentTest2 {
10     
11     public static void main(String[] args) {
12         SessionFactory sessionFactory=HibernateUtil.getSessionFactory();
13         Session session=sessionFactory.openSession(); // 生成一个session
14         session.beginTransaction(); // 开启事务
15         
16         Student s=new Student();
17         s.setName("张三");
18         session.save(s);
19         
20         session.getTransaction().commit(); // 提交事务
21         session.close(); // 关闭session
22     }
23 }

 

结果显示:

当我继续在执行一下代码。。。。。

结果显示:

 

identity 例子:

increment 例子的代码只改变一下Student.hbm.xml,其他什么代码都不改变。

Student.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Student" table="t_student">
 9         <id name="id" column="stuId">
10             <generator class="identity"></generator>
11         </id>
12         
13         <property name="name"></property>
14     </class>
15 
16 </hibernate-mapping>

 

结果显示:

当我继续在执行一下代码。。。。。

结果显示:

 

native 例子:

increment 例子的代码只改变一下Student.hbm.xml,其他什么代码都不改变。

Student.hbm.xml

 1 <?xml version="1.0"?>
 2 <!DOCTYPE hibernate-mapping PUBLIC
 3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
 5 
 6 <hibernate-mapping package="com.wishwzp.model">
 7 
 8     <class name="Student" table="t_student">
 9         <id name="id" column="stuId">
10             <generator class="native"></generator>
11         </id>
12         
13         <property name="name"></property>
14     </class>
15 
16 </hibernate-mapping>

 

结果显示:

当我继续在执行一下代码。。。。。

结果显示:

 

主要native用的最多。

 

posted @ 2016-05-12 00:13  kika  阅读(1140)  评论(0编辑  收藏  举报