现在大家都不推荐使用联合主键,关键是因为其需要自己手工维护,比较麻烦。但是一个项目可能因为历史遗留原因,你不得不面对联合主键。
Hibernate联合主键问题解决如下:
可以使用一个组件作为一个实体类的标识符。你的组件类必须满足以下要求:
(1)它必须实现 java.io.Serializable 接口
(2)它必须重新实现 equals() 和 hashCode() 方法,始终和组合关键字在数据库中的概念保持一致
注意:在 Hibernate3 中,第二个要求并非是 Hibernate 强制必须的。但最好这样做。
不能使用一个 IdentifierGenerator 产生组合关键字。一个应用程序必须分配它自己的标识符。
将主键所对应属性提取出一个类(称之为主键类),并且主键类需要实现Serializable接口,重写equals方法与hashCode方法,原因:
在于Hibernate要根据数据库的联合主键来判断某两行记录是否是一样,如果一样那么就认为是同一个对象,如果不一样,那么就认为是不同的对象。
具体做法如下步骤:
(1)将主键所对应属性提取出一个类(称之为主键类),并且主键类需要实现Serializable接口,重写equals方法与hashCode方法
实例如下:
package com.model; import java.io.Serializable; //ScoreInfo主键类 //composite-id class must implement Serializable: com.model.ScorePK public class ScorePK implements Serializable{ private static final long serialVersionUID = 1L; private Integer studentID; private Integer courseID; public int getStudentID() { return studentID; } public void setStudentID(int studentID) { this.studentID = studentID; } public int getCourseID() { return courseID; } public void setCourseID(int courseID) { this.courseID = courseID; } @Override public boolean equals(Object o){ if(o instanceof ScorePK){ ScorePK scorePK = (ScorePK)o; if(this.studentID == scorePK.studentID && this.courseID == scorePK.courseID){ return true; } } return false; } @Override public int hashCode(){ int result = 1; result = result + ((this.studentID == null) ? 0 : this.studentID.hashCode()); result = result + ((this.courseID == null) ? 0 : this.courseID.hashCode()); return result; } }
(2)实体类(这里不用包含主键所对应的属性)
package com.model; public class ScoreInfo { //联合主键 private ScorePK scorePK; private int score; public ScorePK getScorePK() { return scorePK; } public void setScorePK(ScorePK scorePK) { this.scorePK = scorePK; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.model.ScoreInfo" table="ScoreInfo"> <composite-id name = "ScorePK" class = "com.model.ScorePK"> <key-property name="StudentID"></key-property> <key-property name="CourseID"></key-property> </composite-id> <property name="score" column = "Score"/> </class> </hibernate-mapping>如果表使用联合主键,你可以映射类的多个属性为标识符属性。 <composite-id>元素接受<key-property> 属性映射和<key-many-to-one>属性映射作为子元素。
<composite-id name = "ScorePK" class = "com.model.ScorePK"> <key-property name="StudentID"></key-property> <key-property name="CourseID"></key-property> </composite-id>
(4)不要忘记在hibernate配置文件中引用映射文件。
<!-- XML配置方式 --> <mapping resource="com/model/ScoreInfo.hbm.xml"/>(5)测试
package com.test; import org.hibernate.Session; import com.model.ScoreInfo; import com.model.ScorePK; import com.util.HibernateUtil; public class Test { public static void main(String[] args) { Test mgr = new Test(); mgr.createAndStoreEvent(); HibernateUtil.getSessionFactory().close(); } private void createAndStoreEvent() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); ScorePK scorePK = new ScorePK(); scorePK.setStudentID(2); scorePK.setCourseID(1); ScoreInfo score = new ScoreInfo(); score.setScorePK(scorePK); score.setScore(98); session.save(score); session.getTransaction().commit(); } }
package com.util; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { private static final SessionFactory sessionFactory; static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
下面是定义联合主键的几种方式:
(1)将组件类(主键类)注解为@Embeddable,并在实体类中将组件的属性注解为@Id
(2)在实体类中将组件的属性注解为@EmbeddedId
(3)将实体类注解为@IdClass(value = ScorePk.class),并将该实体中所有属于主键的属性都注解为@Id
对于EJB2的开发人员来说@IdClass是很常见的,但是对于Hibernate用户来说就是一个崭新的用法。
组合主键类对应一个实体类的多个字段或属性,而且主键类中定义主键的字段或属性和实体类中对应的字段或属性早类型上必须一致。
实例如下:
(1)将组件类(主键类)注解为@Embeddable
package com.model; import java.io.Serializable; import javax.persistence.Embeddable; //ScoreInfo主键类 //composite-id class must implement Serializable: com.model.ScorePK @Embeddable public class ScorePK implements Serializable{ private static final long serialVersionUID = 1L; private Integer studentID; private Integer courseID; public int getStudentID() { return studentID; } public void setStudentID(int studentID) { this.studentID = studentID; } public int getCourseID() { return courseID; } public void setCourseID(int courseID) { this.courseID = courseID; } @Override public boolean equals(Object o){ if(o instanceof ScorePK){ ScorePK scorePK = (ScorePK)o; if(this.studentID == scorePK.studentID && this.courseID == scorePK.courseID){ return true; } } return false; } @Override public int hashCode(){ int result = 1; result = result + ((this.studentID == null) ? 0 : this.studentID.hashCode()); result = result + ((this.courseID == null) ? 0 : this.courseID.hashCode()); return result; } }
将在实体类中组件的属性注解为@Id
public class ScoreInfo { private int studentID; private ScorePK scorePK; @Id public ScorePK getScorePK() { return scorePK; } public void setScorePK(ScorePK scorePK) { this.scorePK = scorePK; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
(2)在实体类中将组件的属性注解为@EmbeddedId
public class ScoreInfo { private int studentID; private ScorePK scorePK; @EmbeddedId public ScorePK getScorePK() { return scorePK; } public void setScorePK(ScorePK scorePK) { this.scorePK = scorePK; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }
(3)将实体类注解为@IdClass(value = ScorePk.class),并将该实体中所有属于主键的属性都注解为@Id
@Entity @IdClass(value = ScorePk.class) public class ScoreInfo { private int studentID; private int courseID; private int score; @Id public int getStudentID() { return studentID; } public void setStudentID(int studentID) { this.studentID = studentID; } @Id public int getCourseID() { return courseID; } public void setCourseID(int courseID) { this.courseID = courseID; } public int getScore() { return score; } public void setScore(int score) { this.score = score; } }