【发布时间】:2014-06-18 14:02:40
【问题描述】:
我在数据库中有一个两列主键,我需要使用 hibernate 4.2 和 jpa 使用 spring mvc 应用程序对其进行建模。从我在网上阅读的内容看来,我的复合键类ConceptPK 必须包含一个哈希码方法。问题是主键的两个元素之一是 BigInteger 数据类型,但 hashcode() 方法的默认返回类型是 int。这导致 eclipse 在下面给出一条错误消息,指示程序将无法编译,因为我的 hashcode 方法的返回类型错误。
我需要hashcode 方法吗?我必须对下面的代码做什么才能使用功能正常的复合键 ConceptPK 进行编译?
import java.io.Serializable;
import java.math.BigInteger;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;
@Embeddable
class ConceptPK implements Serializable {
@Column(name="id", nullable=false)
protected BigInteger id;
@Column(name="effectiveTime", nullable=false)
@Type(type="org.jadira.usertype.dateandtime.joda.PersistentDateTime")
private DateTime effectiveTime;
public ConceptPK() {}
public ConceptPK(BigInteger bint, DateTime dt) {
this.id = bint;
this.effectiveTime = dt;
}
/** getters and setters **/
public DateTime getEffectiveTime(){return effectiveTime;}
public void setEffectiveTime(DateTime ad){effectiveTime=ad;}
public void setId(BigInteger id) {this.id = id;}
public BigInteger getId() {return id;}
public boolean equals(Object o) {
return ((o instanceof ConceptPK) &&
effectiveTime.equals(((ConceptPK)o).getEffectiveTime()) &&
id == ((ConceptPK) o).getId());
}
public int hashCode() {
BigInteger sum = BigInteger.valueOf(
effectiveTime.hashCode()
);
sum.add(id);
return sum;//this line has error message indicating wrong return data type
}
}
以下是使用 ConceptPK 作为其主键的类的代码:
@Entity
@Table(name = "tablename")
public class Concept implements Serializable{
@EmbeddedId
@AttributeOverrides({
@AttributeOverride(name="id", column=@Column(name="id")),
@AttributeOverride(name="effectiveTime", column=@Column(name="effectiveTime"))
})
private ConceptPK conceptPK;
//lots of other stuff
}
【问题讨论】:
标签: java spring hibernate spring-mvc jpa