hibernate映射文件中的id标签以及联合主键
一、id标签
被映射的类必须定义对应数据库表主键字段。大多数类有一个JavaBeans风格的属性, 为每一个实例包含唯一的标识。<id> 元素定义了该属性到数据库表主键字段的映射
二、联合场景
学生表Student是由id和name联合形成主键
三、联合主键类的编写
1)实现java.io.Serializable接口
2)必须重写equals() 和 hashCode() 方法
package com.linys.model; /** * StudentId entity. @author MyEclipse Persistence Tools */ public class StudentId implements java.io.Serializable { // Fields /** * */ private static final long serialVersionUID = 1L; private Integer id; private String name; // Constructors /** default constructor */ public StudentId() { } /** full constructor */ public StudentId(Integer id, String name) { this.id = id; this.name = name; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof StudentId)) return false; StudentId castOther = (StudentId) other; return ((this.getId() == castOther.getId()) || (this.getId() != null && castOther.getId() != null && this.getId().equals( castOther.getId()))) && ((this.getName() == castOther.getName()) || (this.getName() != null && castOther.getName() != null && this.getName() .equals(castOther.getName()))); } public int hashCode() { int result = 17; result = 37 * result + (getId() == null ? 0 : this.getId().hashCode()); result = 37 * result + (getName() == null ? 0 : this.getName().hashCode()); return result; } }
三、学生类的编写
package com.linys.model; /** * Student entity. @author MyEclipse Persistence Tools */ public class Student implements java.io.Serializable { // Fields /** * */ private static final long serialVersionUID = 1L; private StudentId id; private Integer age; // Constructors /** default constructor */ public Student() { } /** minimal constructor */ public Student(StudentId id) { this.id = id; } /** full constructor */ public Student(StudentId id, Integer age) { this.id = id; this.age = age; } // Property accessors public StudentId getId() { return this.id; } public void setId(StudentId id) { this.id = id; } public Integer getAge() { return this.age; } public void setAge(Integer age) { this.age = age; } }
四、映射文件
Student.hbm.xml
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Mapping file autogenerated by MyEclipse Persistence Tools --> <hibernate-mapping> <class name="com.linys.model.Student" table="student"> <composite-id name="id" class="com.linys.model.StudentId"> <key-property name="id" type="java.lang.Integer"> <column name="id" /> </key-property> <key-property name="name" type="java.lang.String"> <column name="name" length="50" /> </key-property> </composite-id> <property name="age" type="java.lang.Integer"> <column name="age" /> </property> </class> </hibernate-mapping>
其中:
<composite-id 指定联合主键的信息
<key-property 指定在StudentId类中的联合信息
五、测试类
package com.linys.model; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; public class TestUniPk { static SessionFactory sf; @BeforeClass public static void setUpBeforeClass() throws Exception { sf = new Configuration().configure().buildSessionFactory(); } @Test public void testUniPk(){ StudentId stuId=new StudentId(); //stuId.setId(1); stuId.setName("linys"); Student stu=new Student(); stu.setAge(23); stu.setId(stuId); Session session =sf.openSession(); Transaction tr=session.beginTransaction(); session.save(stu); tr.commit(); session.save(stu); } @AfterClass public static void tearDownAfterClass() throws Exception { sf.close(); } }