复合(联合)主键映射的时候,通常将复合主键相关的属性,单独放到一个类中,那么需要满足以下条件
①此类必须实现序列化接口 implements java.io.Serializable
②覆写hashcode和equals方法
类:
import java.io.Serializable;
// 主键类
public class NamePK implements Serializable{
private static final long serialVersionUID = 8313037022835155821L;
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
// hashcode和equals方法交给交给eclipse自己来写,source/generator hashCode() and equals()
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((firstName == null) ? 0 : firstName.hashCode());
result = PRIME * result + ((lastName == null) ? 0 : lastName.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final NamePK other = (NamePK) obj;
if (firstName == null) {
if (other.firstName != null)
return false;
} else if (!firstName.equals(other.firstName))
return false;
if (lastName == null) {
if (other.lastName != null)
return false;
} else if (!lastName.equals(other.lastName))
return false;
return true;
}
}
// 实体类
public class UserInfo {
private NamePK namePk;
private String sex;
private int age;
}
hbm.xml
UserInfo.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 package="com.zd.model">
<class name="UserInfo" table="user_info" >
<composite-id name="namePk">
<key-property name="firstName" column="first_name" length="12"/>
<key-property name="lastName" column="last_name" length="12"/>
</composite-id>
<property name="sex" length="12"/>
<property name="age" />
</class>
</hibernate-mapping>
测试用例:
public void testCompositeId(){
Session session = null;
Transaction ta = null;
try{
session = HibernateUtil.getSession();
ta = session.beginTransaction();
NamePK namePk = new NamePK();
namePk.setFirstName("Zhang");
namePk.setLastName("san");
UserInfo ui = new UserInfo();
ui.setNamePk(namePk);
ui.setSex("M");
ui.setAge(12);
session.save(ui);
ta.commit();
}catch(Exception e){
e.printStackTrace();
if(ta != null){
ta.rollback();
}
}finally{
//关闭session,user变为detached离线对象
HibernateUtil.closeSession(session);
}
}
public void testgetId(){
Session session = null;
Transaction ta = null;
try{
session = HibernateUtil.getSession();
ta = session.beginTransaction();
NamePK namePk = new NamePK();
namePk.setFirstName("Zhang");
namePk.setLastName("san");
//get or load均是要序列化的类,所以要把主键implements Serializable
UserInfo ui = (UserInfo) session.get(UserInfo.class, namePk);
System.out.println(ui.getAge());
System.out.println(ui.getSex());
ta.commit();
}catch(Exception e){
e.printStackTrace();
if(ta != null){
ta.rollback();
}
}finally{
//关闭session,user变为detached离线对象
HibernateUtil.closeSession(session);
}
}
原帖地址:http://hi.baidu.com/fytcm/blog/item/5124737b38ea56fe0bd187fa.html