hibernate映射文件中的id标签以及联合主键

来源:互联网 发布:西安软件新城二期工程 编辑:程序博客网 时间:2024/05/17 22:31

一、id标签

       被映射的类必须定义对应数据库表主键字段。大多数类有一个JavaBeans风格的属性, 为每一个实例包含唯一的标识<id> 元素定义了该属性到数据库表主键字段的映射

 

二、联合场景

学生表Student是由id和name联合形成主键

三、联合主键类的编写

      1)实现java.io.Serializable接口

      2)必须重写equals() 和 hashCode() 方法

 

Java代码  
  1. package com.linys.model;  
  2.   
  3. /** 
  4.  * StudentId entity. @author MyEclipse Persistence Tools 
  5.  */  
  6.   
  7. public class StudentId implements java.io.Serializable {  
  8.   
  9.     // Fields  
  10.   
  11.     /** 
  12.      *  
  13.      */  
  14.     private static final long serialVersionUID = 1L;  
  15.     private Integer id;  
  16.     private String name;  
  17.   
  18.     // Constructors  
  19.   
  20.     /** default constructor */  
  21.     public StudentId() {  
  22.     }  
  23.   
  24.     /** full constructor */  
  25.     public StudentId(Integer id, String name) {  
  26.         this.id = id;  
  27.         this.name = name;  
  28.     }  
  29.   
  30.     // Property accessors  
  31.   
  32.     public Integer getId() {  
  33.         return this.id;  
  34.     }  
  35.   
  36.     public void setId(Integer id) {  
  37.         this.id = id;  
  38.     }  
  39.   
  40.     public String getName() {  
  41.         return this.name;  
  42.     }  
  43.   
  44.     public void setName(String name) {  
  45.         this.name = name;  
  46.     }  
  47.   
  48.     public boolean equals(Object other) {  
  49.         if ((this == other))  
  50.             return true;  
  51.         if ((other == null))  
  52.             return false;  
  53.         if (!(other instanceof StudentId))  
  54.             return false;  
  55.         StudentId castOther = (StudentId) other;  
  56.   
  57.         return ((this.getId() == castOther.getId()) || (this.getId() != null  
  58.                 && castOther.getId() != null && this.getId().equals(  
  59.                 castOther.getId())))  
  60.                 && ((this.getName() == castOther.getName()) || (this.getName() != null  
  61.                         && castOther.getName() != null && this.getName()  
  62.                         .equals(castOther.getName())));  
  63.     }  
  64.   
  65.     public int hashCode() {  
  66.         int result = 17;  
  67.   
  68.         result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());  
  69.         result = 37 * result  
  70.                 + (getName() == null ? 0 : this.getName().hashCode());  
  71.         return result;  
  72.     }  
  73.   
  74. }  

三、学生类的编写

 

Java代码  
  1. package com.linys.model;  
  2.   
  3. /** 
  4.  * Student entity. @author MyEclipse Persistence Tools 
  5.  */  
  6.   
  7. public class Student implements java.io.Serializable {  
  8.   
  9.     // Fields  
  10.   
  11.     /** 
  12.      *  
  13.      */  
  14.     private static final long serialVersionUID = 1L;  
  15.     private StudentId id;  
  16.     private Integer age;  
  17.   
  18.     // Constructors  
  19.   
  20.     /** default constructor */  
  21.     public Student() {  
  22.     }  
  23.   
  24.     /** minimal constructor */  
  25.     public Student(StudentId id) {  
  26.         this.id = id;  
  27.     }  
  28.   
  29.     /** full constructor */  
  30.     public Student(StudentId id, Integer age) {  
  31.         this.id = id;  
  32.         this.age = age;  
  33.     }  
  34.   
  35.     // Property accessors  
  36.   
  37.     public StudentId getId() {  
  38.         return this.id;  
  39.     }  
  40.   
  41.     public void setId(StudentId id) {  
  42.         this.id = id;  
  43.     }  
  44.   
  45.     public Integer getAge() {  
  46.         return this.age;  
  47.     }  
  48.   
  49.     public void setAge(Integer age) {  
  50.         this.age = age;  
  51.     }  
  52.   
  53. }  
 

四、映射文件

Student.hbm.xml

 

Xml代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!--  
  5.     Mapping file autogenerated by MyEclipse Persistence Tools 
  6. -->  
  7. <hibernate-mapping>  
  8.     <class name="com.linys.model.Student" table="student">  
  9.         <composite-id name="id" class="com.linys.model.StudentId">  
  10.             <key-property name="id" type="java.lang.Integer">  
  11.                 <column name="id" />  
  12.                   
  13.             </key-property>  
  14.             <key-property name="name" type="java.lang.String">  
  15.                 <column name="name" length="50" />  
  16.             </key-property>  
  17.         </composite-id>  
  18.         <property name="age" type="java.lang.Integer">  
  19.             <column name="age" />  
  20.         </property>  
  21.     </class>  
  22. </hibernate-mapping>  
 

其中:

<composite-id 指定联合主键的信息

 <key-property 指定在StudentId类中的联合信息

五、测试类

 

 

Java代码  
  1. package com.linys.model;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.Transaction;  
  6. import org.hibernate.cfg.Configuration;  
  7. import org.junit.AfterClass;  
  8. import org.junit.BeforeClass;  
  9. import org.junit.Test;  
  10.   
  11. public class TestUniPk {  
  12.   
  13.     static SessionFactory sf;  
  14.   
  15.     @BeforeClass  
  16.     public static void setUpBeforeClass() throws Exception {  
  17.         sf = new Configuration().configure().buildSessionFactory();  
  18.   
  19.     }  
  20.   
  21.     @Test  
  22.     public void testUniPk(){  
  23.         StudentId stuId=new StudentId();  
  24.         //stuId.setId(1);  
  25.         stuId.setName("linys");  
  26.           
  27.         Student stu=new Student();  
  28.         stu.setAge(23);  
  29.         stu.setId(stuId);  
  30.           
  31.         Session session =sf.openSession();  
  32.         Transaction tr=session.beginTransaction();  
  33.         session.save(stu);  
  34.         tr.commit();  
  35.         session.save(stu);  
  36.           
  37.           
  38.     }  
  39.     @AfterClass  
  40.     public static void tearDownAfterClass() throws Exception {  
  41.         sf.close();  
  42.     }  
  43.   
  44. }  

    声明:此文转载于iteye,文章仅用于交流学习等不以盈利为目的使用。
0 0
原创粉丝点击