User类
package com.lbx.model;
import java.util.Date;
//联合主键的生成
public class User {
/*private int id;
private String name;*/
private UserPK userPK;
private Date birthday;
/*public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}*/
public Date getBirthday() {
return birthday;
}
public UserPK getUserPK() {
return userPK;
}
public void setUserPK(UserPK userPK) {
this.userPK = userPK;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public User() {
super();
}
public User(Date birthday) {
super();
this.birthday = birthday;
}
public User(UserPK userPK, Date birthday) {
super();
this.userPK = userPK;
this.birthday = birthday;
}
}
User主键类UserPK
package com.lbx.model;
import java.io.Serializable;
@SuppressWarnings("serial")
public class UserPK implements Serializable{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o){
if(o instanceof UserPK){
UserPK userPK = (UserPK)o;
if(this.id==userPK.getId()&&this.name.equals(userPK.getName())){
return true;
}
}
return false;
}
@Override
public int hashCode(){
return this.name.hashCode();
}
}
User.hbm.xml文件的配置
<?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 package="com.lbx.model"> <class name="User" table="user" lazy="true"> <composite-id name="userPK" class="UserPK"> <key-property name="id" column="ID" type="integer"></key-property> <key-property name="name" column="NAME" type="string"></key-property> </composite-id> <property name="birthday" type="java.util.Date" /> </class> </hibernate-mapping>
测试方法和hibernate.cfg.xml文件没写(很简单)