如果数据库中用多个字段而不仅仅是一个字段作为主键,也就是联合主键,这个时候就可以使用hibernate提供的联合主键生成策略。
具体如下:
可以使用一个组件作为一个实体类的标识符。你的组件类必须满足以下要求:
它必须实现 java.io.Serializable 接口
它必须重新实现 equals() 和 hashCode() 方法,始终和组合关键字在数据库中的概念保持一致
注意:在 Hibernate3 中,第二个要求并非是 Hibernate 强制必须的。但最好这样做。
不能使用一个 IdentifierGenerator 产生组合关键字。一个应用程序必须分配它自己的标识符。
具体做法如下步骤:
1.写一个包含表中联合主键的所有字段的类,作为主键类,实例如下:
package com.seed.lee.model;
/**
* 这个类作为Person类的(组合主键)主键类
*
* @author Administrator
*
*/
public class PersonUionPKID implements java.io.Serializable {
private String firstName;
private String secondName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof PersonUionPKID){
PersonUionPKID pk=(PersonUionPKID)obj;
if(this.firstName.equals(pk.firstName)&&this.secondName.equals(pk.secondName)){
return true;
}
}
return false;
}
@Override
public int hashCode() {
return super.hashCode();
}
}
这里要特别注意,如hibernate API文档所述,主键类必须实现java.io.Serializable接口,而且推荐用自己的方法override equals() 和hashCode()方法,保证主键的唯一性.
2.再写一个类与表字段相当的类,这个类里面不一定要包含表主键的所有字段,实例如下:
package com.seed.lee.model;
public class Person {
PersonUionPKID uionPKID = new PersonUionPKID();
private int age;
private String sex;
private String job;
public PersonUionPKID getUionPKID() {
return uionPKID;
}
public void setUionPKID(PersonUionPKID uionPKID) {
this.uionPKID = uionPKID;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
3.配置hibernate配置文件,实例如下:
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
在使用annotation的时候,有三种方法来实现联合组建:
1.将组件类注解为@Embeddable,并将组件的属性注解为@Id
package com.seed.lee.model;
/**
* 这个类作为Person类的(组合主键)主键类
*
* @author Administrator
*
*/
@Embeddable
public class PersonUionPKID implements java.io.Serializable {
private String firstName;
private String secondName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
@Override
public boolean equals(Object obj) {
if(obj instanceof PersonUionPKID){
PersonUionPKID pk=(PersonUionPKID)obj;
if(this.firstName.equals(pk.firstName)&&this.secondName.equals(pk.secondName)){
return true;
}
}
return false;
}
@Override
public int hashCode() {
return super.hashCode();
}
}
package com.seed.lee.model;
public class Person {
PersonUionPKID uionPKID = new PersonUionPKID();
private int age;
private String sex;
private String job;
@Id
public PersonUionPKID getUionPKID() {
return uionPKID;
}
public void setUionPKID(PersonUionPKID uionPKID) {
this.uionPKID = uionPKID;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
2. 将组件的属性注解为@EmbeddedId
package com.seed.lee.model;
public class Person {
PersonUionPKID uionPKID = new PersonUionPKID();
private int age;
private String sex;
private String job;
@EmbeddedId
public PersonUionPKID getUionPKID() {
return uionPKID;
}
public void setUionPKID(PersonUionPKID uionPKID) {
this.uionPKID = uionPKID;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}
3.将类注解为@IdClass,并将该实体中所有属于主键的属性都注解为@Id
package com.seed.lee.model;
@@IdClass(PersonUionPKID.class)
public class Person {
private String firstName;
private String secondName;
private int age;
private String sex;
private String job;
@Id
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Id
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
}