Hibernate inserts duplicates into a @OneToMany collection

前端 未结 5 1633
野性不改
野性不改 2020-12-01 20:52

I have a question concerning Hibernate 3.6.7 and JPA 2.0.

Consider following entities (some getters and setters are omitted for brevity):

@Entity
pub         


        
5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-01 21:40

    I fixed this problem by telling Hibernate not to add duplicates in my collection. In your case change the type of your children field from List to Set and implement equals(Object obj) and hashCode() on the Child class.

    Obviously this will not be possible in every case, but if there is a sane way to identify that a Child instance is unique then this solution can be relatively painless.

提交回复
热议问题