hibernate注解办法实现复合主键[Java编程]
本文“hibernate注解办法实现复合主键[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:
有时一个实体的主键大概同时为多个,比方一样是之前利用的“CustomerEO”实体,需求通过name和email来查找指定实体,当且仅当name和email的值完好相同时,才认为是相同的实体对象.要配置这样的复合主键,步骤如以下所示.
(1)编写一个复合主键的类CustomerPK,代码以下.
CustomerPK.java
import java.io.Serializable;
public class CustomerPK implements Serializable {
public CustomerPK() {
}
public CustomerPK(String name, String email ) {
this.name = name;
this.email = email ;
}
private String email ;
public String getEmail () {
return email ;
}
public void setEmail (String email ) {
this.email = email ;
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME * result + ((email == null ) ? 0 : email .hashCode());
result = PRIME * result + ((name == null ) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null )
return false;
if (getClass() != obj.getClass())
return false;
final CustomerPK other = (CustomerPK) obj;
if (email == null ) {
if (other.email != null )
return false;
} else if (!email .equals(other.email ))
return false;
if (name == null ) {
if (other.name != null )
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
}
以上是“hibernate注解办法实现复合主键[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
本文地址: | 与您的QQ/BBS好友分享! |