我有 JPA2/Hibernate 的设置。此外,这些实体由 Hibernate Envers 审计。我有以下类来表示只有一个字段的邮政编码,即value
.
当仅将任何邮政编码的一个实例保存到Set<PostalCode>
. 但是,当我尝试添加另一个邮政编码时,审核失败并出现以下异常:
Caused by: javax.persistence.EntityExistsException: a different object with the same identifier value was already associated with the session: [PostalCodesAudit#{PostOffice_id=5, revision_id=DefaultRevisionEntity(id = 16, revisionDate = Aug 19, 2013 8:50:05 AM), revisionType=ADD}]
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1359)
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:80)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:513)
... 58 more
这里奇怪的是它工作正常,当我从类中删除实现时equals
,该机制工作正常。此外,如果我将集合更改为,类似的映射将完美运行。hashCode
PostalCode
Set<String>
我并不是绝对需要在课堂上实施equals
和实施,但我也不太热衷于放弃支持。hashCode
PostalCode
映射这种情况的正确方法是什么?
实体
@Embeddable
public class PostalCode {
@Column(name = "postalCode", length = 5)
@Pattern(regexp = "[0-9}{5}")
private String value;
// Getter and setter for value
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof PostalCode)) {
return false;
}
PostalCode that = (PostalCode) o;
return new EqualsBuilder().append(this.value, that.value).isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(this.value).toHashCode();
}
}
在另一堂课中,我尝试将其用作@ElementCollection
:
@Valid
@ElementCollection(targetClass = PostalCode.class)
@CollectionTable(name="PostalCodes", joinColumns = @JoinColumn(name = "postOffice_id"))
private Set<PostalCode> postalCodes = new HashSet<PostalCode>();
编辑 1
进一步调查显示,如果集合的类型是Collection<PostalCode>
或List<PostalCode>
,即使没有实现,审计也会失败equals/hashCode
:
// No difference in the following
private Collection<PostalCode> postalCodes = new HashSet<PostalCode>();
private Collection<PostalCode> postalCodes = new ArrayList<PostalCode>();
re 并且进一步说明,EntityExistsException
throw 不包含邮政编码的值:仅包括外键、revision_id 和 revisionType。
// Excerpt from the exception
PostalCodesAudit#{
PostOffice_id=5,
revision_id=DefaultRevisionEntity(id = 16, revisionDate = Aug 19, 2013 11:52:14 AM),
revisionType=ADD
}
此外,在将字段更改为List<PostalCode>
并使用审核机制对其进行注释后@OrderColumn
,无论是否equals/hashCode
实施,审核机制都可以正常工作。没有订单列它仍然会中断
@ElementCollection(targetClass = PostalCode.class, fetch = FetchType.LAZY)
@CollectionTable(name = "PostOfficePostalCodes", joinColumns = @JoinColumn(name = "postOffice_id"))
@OrderColumn(name = "listOrder")
private List<PostalCode> postalCodes = new ArrayList<PostalCode>();