我正在尝试更新与另一个表有多对多关系的实体。
@Entity(name = "coaching")
public class CoachingEntity {
@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.REFRESH })
@JoinTable(name = "coaching_field", joinColumns = @JoinColumn(name = "coachingId", referencedColumnName = "coachingId"), inverseJoinColumns = @JoinColumn(name = "fieldId", referencedColumnName = "fieldId"))
private Set<FieldEntity> fieldEntityList;
现在,当我更新CoachingEntity时,hibernate正在从coaching_field表中删除条目(多对多连接表)。我在互联网上搜索过(JPA update many-to-many deleting records),但我无法找到正确的方法。即使在JPA many to many merge on the owner triggers delete on join table中,我尝试了他使用set的推荐方法,但我遇到了同样的问题。根据{{3}},我是否需要覆盖equals和hashcode?
答案 0 :(得分:2)
您需要在实体中覆盖equals()和hashCode(),否则您将会遇到问题,特别是如果您的实体是Set的一部分。
详情请见https://community.jboss.org/wiki/EqualsAndHashCode
如果在覆盖equals()和hashCode()之后问题仍然存在,并且您正在使用双向关系;然后,您可能需要在关系的另一侧添加mappedBy并在持久化之前“链接”两个对象。请查看此答案以获取更多详细信息:Hibernate (4.1.2) and Spring (3.1.2) – ManyToMany relationship does not store records in JoinTable
答案 1 :(得分:-1)
也许,您必须将fetchType.LAZY更改为fetchType.EAGER。
如果对象中有集合,则有两个选项:
++