我们知道某些表的主键并不是某个字段唯一确定的,而是由多个字段确定,这叫做联合主键,那么hibernate怎样帮我们实现联合主键呢?
要想实现联合主键,实体类必须有两个约束条件:
1:实体类必须实现Serializable接口。
2:实体类必须实现equals方法和hashCode方法。实现这两个方法的原因是帮助hibernate判断这个实体对象是否已经在表中存在。
类的配置文件中用
<composite-id>
<key-property name="sno" column="sno" type="java.lang.Long"></key-property>
<key-property name="cno" column="cno" type="java.lang.Long"></key-property>
</composite-id>
<composite-id>标签来声明联合主键。
主键列用<key-property>来声明,有几个主属性声明几次。
下面是完整的代码示例。
StudentGrade,学生成绩类,有sno(学号),cno(课程号),grade(成绩),主键是 sno和cno作为联合主键。
package com.shizhan.po;
import java.io.Serializable;
public class StudentGrade implements Serializable{
private Long sno ;
private Long cno ;
private int grade ;
public Long getSno() {
return sno;
}
public void setSno(Long sno) {
this.sno = sno;
}
public Long getCno() {
return cno;
}
public void setCno(Long cno) {
this.cno = cno;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cno == null) ? 0 : cno.hashCode());
result = prime * result + ((sno == null) ? 0 : sno.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
StudentGrade other = (StudentGrade) obj;
if (cno == null) {
if (other.cno != null)
return false;
} else if (!cno.equals(other.cno))
return false;
if (sno == null) {
if (other.sno != null)
return false;
} else if (!sno.equals(other.sno))
return false;
return true;
}
}
StudentGrade.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">
<hibernate-mapping>
<class name="com.shizhan.po.StudentGrade" table="STUDENTGrade" schema="SCOTT">
<composite-id>
<key-property name="sno" column="sno" type="java.lang.Long"></key-property>
<key-property name="cno" column="cno" type="java.lang.Long"></key-property>
</composite-id>
<property name="grade" type="int"></property>
</class>
</hibernate-mapping>
create表的ddl语句
create table SCOTT.STUDENTGrade (
sno number(19,0) not null,
cno number(19,0) not null,
grade number(10,0),
primary key (sno, cno)
)
primary(sno,cno)确实符合的要求。