复合主键,由多个字段组成主键,例如,使用一个用户的firstname和lastname组成主键。
可以通过两种方式确定主键,一种是基于实体类的复合主键,另一种是通过定义主键类来实现。
不管通过哪种方式,复合主键都需要实现equals方法和hashcode方法,以作为不同数据之间是别的标志。
一.基于实体类属性的复合主键
主键由实体类中的属性组成。
1.映射文件TUser.hbm.xml
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
>
-
<
class
name
=
"cn.blogjava.start.TUser"
table
=
"t_user"
catalog
=
"sample"
>
-
<
composite-id
>
-
<
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.TUser.java
java 代码
-
package
cn.blogjava.start;
-
-
import
org.apache.commons.lang.builder.EqualsBuilder;
-
import
org.apache.commons.lang.builder.HashCodeBuilder;
-
-
-
-
-
-
public
class
TUser
implements
java.io.Serializable {
-
-
-
-
private
Integer age;
-
-
private
String firstname;
-
-
private
String lastname;
-
-
public
Integer getAge() {
-
return
age;
-
}
-
-
public
void
setAge(Integer age) {
-
this
.age = age;
-
}
-
-
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
TUser)) {
-
return
false
;
-
}
-
-
TUser user = (TUser)obj;
-
return
new
EqualsBuilder()
-
.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
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
;
-
-
-
-
-
protected
void
setUp() {
-
try
{
-
-
-
-
-
-
-
-
-
-
-
-
-
Configuration config =
new
Configuration().configure();
-
SessionFactory sessionFactory = config.buildSessionFactory();
-
session = sessionFactory.openSession();
-
-
}
catch
(HibernateException e) {
-
-
e.printStackTrace();
-
}
-
}
-
-
-
-
-
-
protected
void
tearDown() {
-
try
{
-
session.close();
-
}
catch
(HibernateException e) {
-
-
e.printStackTrace();
-
}
-
}
-
-
-
-
-
public
void
testInsert() {
-
Transaction tran =
null
;
-
try
{
-
tran = session.beginTransaction();
-
TUser user =
new
TUser();
-
user.setFirstname(
"bai"
);
-
user.setLastname(
"yunfeng"
);
-
user.setAge(
26
);
-
session.save(user);
-
session.flush();
-
tran.commit();
-
}
catch
(HibernateException e) {
-
-
e.printStackTrace();
-
Assert.fail(e.getMessage());
-
if
(tran !=
null
) {
-
try
{
-
tran.rollback();
-
}
catch
(Exception e1) {
-
-
e1.printStackTrace();
-
}
-
}
-
}
-
}
-
-
-
-
-
public
void
testSelect(){
-
TUser user =
new
TUser();
-
user.setFirstname(
"bai"
);
-
user.setLastname(
"yunfeng"
);
-
-
user = (TUser)session.load(TUser.
class
, user);
-
Assert.assertEquals(user.getAge().intValue(),
26
);
-
}
-
}
基于主键类的复合主键:
方法:将主键字段从POJO类中提出了,生成一个主键类。
可以将1中的例子加以改造,将firstname和lastname字段单独提取到一个主键类中。
1.
配置文件TUser.hbm.xml
composite-id节点的name指定了实体类中的主键类的属性名.
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
=
"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;
-
-
-
-
-
-
public
class
TUser
implements
java.io.Serializable {
-
-
-
-
private
Integer age;
-
-
-
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
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();
-
}
-
}
4.测试代码HibernateTest.java
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
;
-
-
-
-
-
protected
void
setUp() {
-
try
{
-
-
-
-
-
-
-
-
-
-
-
-
-
Configuration config =
new
Configuration().configure();
-
SessionFactory sessionFactory = config.buildSessionFactory();
-
session = sessionFactory.openSession();
-
-
}
catch
(HibernateException e) {
-
-
e.printStackTrace();
-
}
-
}
-
-
-
-
-
-
protected
void
tearDown() {
-
try
{
-
session.close();
-
}
catch
(HibernateException e) {
-
-
e.printStackTrace();
-
}
-
}
-
-
-
-
-
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) {
-
-
e.printStackTrace();
-
Assert.fail(e.getMessage());
-
if
(tran !=
null
) {
-
try
{
-
tran.rollback();
-
}
catch
(Exception e1) {
-
-
e1.printStackTrace();
-
}
-
}
-
}
-
}
-
-
-
-
-
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
);
-
}
-
}
posted on 2009-01-05 15:49
JasonChou 阅读(2949)
评论(0) 编辑 收藏 所属分类:
j2ee