Hibernate复合主键的处理(二)
基于主键类的复合主键:
方法:将主键字段从POJO类中提出了,生成一个主键类。
可以将1中的例子加以改造,将firstname和lastname字段单独提取到一个主键类中。
1.
配置文件TUser.hbm.xml
composite-id节点的name指定了实体类中的主键类的属性名。
<?
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 ="cn.blogjava.start" >
< class name ="TUser" table ="t_user" catalog ="sample" >
< composite-id name ="userPK" class ="TUserPK" >
< key-property name ="lastname" column ="lastname" type ="string" />
< key-property name ="firstname" column ="firstname" type ="string" />
</ composite-id >
< property name ="age" type ="integer" column ="age" />
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping package ="cn.blogjava.start" >
< class name ="TUser" table ="t_user" catalog ="sample" >
< composite-id name ="userPK" class ="TUserPK" >
< key-property name ="lastname" column ="lastname" type ="string" />
< key-property name ="firstname" column ="firstname" type ="string" />
</ composite-id >
< property name ="age" type ="integer" column ="age" />
</ class >
</ hibernate-mapping >
2.POJO类
package
cn.blogjava.start;
/**
* TUser generated by hbm2java
*/
public class TUser implements java.io.Serializable {
// Fields
private Integer age;
//配置文件composite-id的name属性
private TUserPK userPK;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this .age = age;
}
public TUserPK getUserPK() {
return userPK;
}
public void setUserPK(TUserPK userPK) {
this .userPK = userPK;
}
}
/**
* TUser generated by hbm2java
*/
public class TUser implements java.io.Serializable {
// Fields
private Integer age;
//配置文件composite-id的name属性
private TUserPK userPK;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this .age = age;
}
public TUserPK getUserPK() {
return userPK;
}
public void setUserPK(TUserPK userPK) {
this .userPK = userPK;
}
}
3.主键类TUserPK.java
package
cn.blogjava.start;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
public class TUserPK implements Serializable {
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;
}
public boolean equals(Object obj) {
if ( ! (obj instanceof TUserPK)) {
return false ;
}
TUserPK userPK = (TUserPK)obj;
return new EqualsBuilder()
.appendSuper( super .equals(obj))
.append( this .lastname, userPK.lastname)
.append( this .firstname, userPK.firstname)
.isEquals();
}
public int hasCode() {
return new HashCodeBuilder( - 528253723 , - 475504089 )
.appendSuper( super .hashCode())
.append( this .lastname).append( this .firstname)
.toHashCode();
}
}
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
public class TUserPK implements Serializable {
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;
}
public boolean equals(Object obj) {
if ( ! (obj instanceof TUserPK)) {
return false ;
}
TUserPK userPK = (TUserPK)obj;
return new EqualsBuilder()
.appendSuper( super .equals(obj))
.append( this .lastname, userPK.lastname)
.append( this .firstname, userPK.firstname)
.isEquals();
}
public int hasCode() {
return new HashCodeBuilder( - 528253723 , - 475504089 )
.appendSuper( super .hashCode())
.append( this .lastname).append( this .firstname)
.toHashCode();
}
}
4.测试代码HibernateTest.java
package
cn.blogjava.start;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest extends TestCase {
Session session = null ;
/**
* JUnit中的setUp方法在TestCase初始化的时候会自动调用
* 一般用于初始化公用资源
*/
protected void setUp() {
try {
/**
* 可以采用hibernate.properties或者hibernate.cfg.xml
* 配置文件的初始化代码
*
* 采用hibernate.properties
* Configuration config = new Configuration();
* config.addClass(TUser.class);
*/
// 采用hibernate.cfg.xml配置文件,与上面的方法对比,两个差异
// 1.Configuration的初始化方式
// 2.xml
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* JUnit中的tearDown方法在TestCase执行完毕的时候会自动调用
* 一般用于释放资源
*/
protected void tearDown() {
try {
session.close();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 对象持久化测试(Insert方法)
*/
public void testInsert() {
Transaction tran = null ;
try {
tran = session.beginTransaction();
TUser user = new TUser();
TUserPK userPK = new TUserPK();
userPK.setFirstname( " yu " );
userPK.setLastname( " yy " );
user.setUserPK(userPK);
user.setAge( 25 );
session.save(user);
session.flush();
tran.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
if (tran != null ) {
try {
tran.rollback();
} catch (Exception e1) {
// TODO: handle exception
e1.printStackTrace();
}
}
}
}
/**
* 对象读取测试(Select方法)
*/
public void testSelect(){
TUserPK userPK = new TUserPK();
userPK.setFirstname( " yu " );
userPK.setLastname( " yy " );
TUser user = (TUser)session.load(TUser. class , userPK);
Assert.assertEquals(user.getAge().intValue(), 25 );
}
}
import junit.framework.Assert;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest extends TestCase {
Session session = null ;
/**
* JUnit中的setUp方法在TestCase初始化的时候会自动调用
* 一般用于初始化公用资源
*/
protected void setUp() {
try {
/**
* 可以采用hibernate.properties或者hibernate.cfg.xml
* 配置文件的初始化代码
*
* 采用hibernate.properties
* Configuration config = new Configuration();
* config.addClass(TUser.class);
*/
// 采用hibernate.cfg.xml配置文件,与上面的方法对比,两个差异
// 1.Configuration的初始化方式
// 2.xml
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* JUnit中的tearDown方法在TestCase执行完毕的时候会自动调用
* 一般用于释放资源
*/
protected void tearDown() {
try {
session.close();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 对象持久化测试(Insert方法)
*/
public void testInsert() {
Transaction tran = null ;
try {
tran = session.beginTransaction();
TUser user = new TUser();
TUserPK userPK = new TUserPK();
userPK.setFirstname( " yu " );
userPK.setLastname( " yy " );
user.setUserPK(userPK);
user.setAge( 25 );
session.save(user);
session.flush();
tran.commit();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
if (tran != null ) {
try {
tran.rollback();
} catch (Exception e1) {
// TODO: handle exception
e1.printStackTrace();
}
}
}
}
/**
* 对象读取测试(Select方法)
*/
public void testSelect(){
TUserPK userPK = new TUserPK();
userPK.setFirstname( " yu " );
userPK.setLastname( " yy " );
TUser user = (TUser)session.load(TUser. class , userPK);
Assert.assertEquals(user.getAge().intValue(), 25 );
}
}