Hibernate之复合主键

先有数据库如下(MS-SQL2005):
-- 创建 person 表

-- 删除 Person 表
DROP TABLE person ;

-- 创建 Person 表
CREATE TABLE person (
name VARCHAR(100) not null ,
phone VARCHAR(50) not null ,
age INT ,
PRIMARY KEY (name,phone)
)

1.向项目的lib目录中添加commons-lang-x.x.x.jar

2.创建Person类,实现 [color=blue]Serializable [/color]接口
复写 equals 和 hashCode 方法
• equals:对象比较方法
• hashCode:取得 Hash 编码


public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (!(obj instanceof Person)) {
return false;
}
PersonKey p = (PersonKey) obj;
return new EqualsBuilder().append(this.name, p.getName()).append(this.phone,
p.getPhone()).isEquals();
}

/**
* Implementation of the hashCode method conforming to the Bloch pattern with
* the exception of array properties (these are very unlikely primary key types).
* @return int
*/
public int hashCode()
{
return new HashCodeBuilder().append(this.name).append(
this.phone).toHashCode();
}


[color=blue]-----Person.hbm.xml----[/color]

<?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" >

<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Sat Nov 11 21:36:21 CST 2006 -->
<hibernate-mapping package="org.lxh.hibernate03">

<class name="Person" table="PERSON">
<composite-id name="id" class="PersonKey">
<key-property name="name" column="NAME" type="string"/>
<key-property name="phone" column="PHONE" type="string"/>
</composite-id>

<property name="age" column="AGE" type="int" />
</class>

</hibernate-mapping>

[color=red]注意class="PersonKey [/color]
新建类[color=blue]PersonKey.java[/color]

package org.lxh.hibernate03;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
public class PersonKey
implements Serializable
{
private volatile int hashValue = 0;
private java.lang.String name;
private java.lang.String phone;
public PersonKey() {}
public java.lang.String getName()
{
return name;
}
public void setName(java.lang.String name)
{
hashValue = 0;
this.name = name;
}
public java.lang.String getPhone()
{
return phone;
}
public void setPhone(java.lang.String phone)
{
hashValue = 0;
this.phone = phone;
}
public boolean equals(Object obj)
{
if (this == obj) {
return true;
}
if (!(obj instanceof Person)) {
return false;
}
PersonKey p = (PersonKey) obj;
return new EqualsBuilder().append(this.name, p.getName()).append(this.phone,
p.getPhone()).isEquals();
}
public int hashCode()
{
return new HashCodeBuilder().append(this.name).append(
this.phone).toHashCode();
}
}



[color=blue]Person.java[/color]


private PersonKey id ;
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public PersonKey getId() {
return id;
}
public void setId(PersonKey id) {
this.id = id;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate 中配置联合主键,可以通过使用 @IdClass 或 @EmbeddedId 注解来实现。 1. 使用 @IdClass 注解: 首先,需要创建一个用于表示联合主键的类,该类需要实现 Serializable 接口,并且需要包含所有联合主键字段。例如: ``` public class MyCompositeKey implements Serializable { private Long id1; private Long id2; // constructors, getters, setters, equals, hashCode, etc. } ``` 然后,在实体类中使用 @IdClass 注解,并指定联合主键类的名称。例如: ``` @Entity @IdClass(MyCompositeKey.class) public class MyEntity { @Id private Long id1; @Id private Long id2; // other entity fields, getters, setters, etc. } ``` 2. 使用 @EmbeddedId 注解: 首先,需要创建一个用于表示联合主键的类,该类需要实现 Serializable 接口,并且需要使用 @Embeddable 注解标记。例如: ``` @Embeddable public class MyCompositeKey implements Serializable { private Long id1; private Long id2; // constructors, getters, setters, equals, hashCode, etc. } ``` 然后,在实体类中使用 @EmbeddedId 注解,并指定联合主键类的名称。例如: ``` @Entity public class MyEntity { @EmbeddedId private MyCompositeKey id; // other entity fields, getters, setters, etc. } ``` 以上就是在 Hibernate 中配置联合主键的两种方式。需要注意的是,使用 @IdClass 注解时需要在实体类中指定所有联合主键字段,而使用 @EmbeddedId 注解时只需要在联合主键类中指定即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值