Hibernate复合主键

复合主键,由多个字段组成主键,例如,使用firstname和lastname组成主键。

可以通过两种方式确定主键,一种是基于实体类的复合主键,另一种是通过定义主键类来实现。
不管通过哪种方式,复合主键都需要实现equals方法和hashcode方法,以作为不同数据之间是别的标志。

一.基于实体类属性的复合主键
主键由实体类中的属性组成。

1.
配置文件TUser.hbm.xml
<? xmlversion = " 1.0 " ?>
<! DOCTYPEhibernate - mappingPUBLIC " -//Hibernate/HibernateMappingDTD3.0//EN "
" http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd " >
< hibernate - mapping >
< class name = " cn.blogjava.start.TUser " table = " t_user " catalog = " sample " >
< composite - id >
< key - propertyname = " lastname " column = " lastname " type = " string " />
< key - propertyname = " firstname " column = " firstname " type = " string " />
</ composite - id >

< propertyname = " age " type = " integer " column = " age " />
</ class >
</ hibernate - mapping >

2.
TUser.java
package cn.blogjava.start;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

/**
*TUsergeneratedbyhbm2java
*/

public class TUser implements java.io.Serializable{

// Fields

private Integerage;

private Stringfirstname;

private Stringlastname;

public IntegergetAge(){
return age;
}

public void setAge(Integerage){
this .age = age;
}

public StringgetFirstname(){
return firstname;
}

public void setFirstname(Stringfirstname){
this .firstname = firstname;
}

public StringgetLastname(){
return lastname;
}

public void setLastname(Stringlastname){
this .lastname = lastname;
}

public boolean equals(Objectobj){
if ( ! (obj instanceof TUser)){
return false ;
}

TUseruser
= (TUser)obj;
return new EqualsBuilder() // EqualsBuilder 和HashCodeBuilder均为apache common lang包中的工具类
.appendSuper(
super .equals(obj))
.append(
this .lastname,user.lastname)
.append(
this .firstname,user.firstname)
.isEquals();
}

public int hasCode(){
return new HashCodeBuilder( - 528253723 , - 475504089 )
.appendSuper(
super .hashCode())
.append(
this .lastname).append( this .firstname)
.toHashCode();

}

}

3.
测试类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{

Sessionsession
= null ;
/**
*JUnit中的setUp方法在TestCase初始化的时候会自动调用
*一般用于初始化公用资源
*/
protected void setUp(){
try {
/**
*可以采用hibernate.properties或者hibernate.cfg.xml
*配置文件的初始化代码
*
*采用hibernate.properties
*Configurationconfig=newConfiguration();
*config.addClass(TUser.class);
*/

// 采用hibernate.cfg.xml配置文件,与上面的方法对比,两个差异
// 1.Configuration的初始化方式
// 2.xml
Configurationconfig = new Configuration().configure();
SessionFactorysessionFactory
= config.buildSessionFactory();
session
= sessionFactory.openSession();

}
catch (HibernateExceptione){
// TODO:handleexception
e.printStackTrace();
}
}

/**
*JUnit中的tearDown方法在TestCase执行完毕的时候会自动调用
*一般用于释放资源
*/
protected void tearDown(){
try {
session.close();
}
catch (HibernateExceptione){
// TODO:handleexception
e.printStackTrace();
}
}

/**
*对象持久化测试(Insert方法)
*/
public void testInsert(){
Transactiontran
= null ;
try {
tran
= session.beginTransaction();
TUseruser
= new TUser();
user.setFirstname(
" bai " );
user.setLastname(
" yunfeng " );
user.setAge(
26 );
session.save(user);
session.flush();
tran.commit();
}
catch (HibernateExceptione){
// TODO:handleexception
e.printStackTrace();
Assert.fail(e.getMessage());
if (tran != null ){
try {
tran.rollback();
}
catch (Exceptione1){
// TODO:handleexception
e1.printStackTrace();
}
}
}
}

/**
*对象读取测试(Select方法)
*/
public void testSelect(){
TUseruser
= new TUser();
user.setFirstname(
" bai " );
user.setLastname(
" yunfeng " );

user
= (TUser)session.load(TUser. class ,user);
Assert.assertEquals(user.getAge().intValue(),
26 );
}
}

你可能感兴趣的:(apache,Hibernate,xml,.net,JUnit)