Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate - NonUniqueObjectException
    text
    copied!<p>I use JPA/Hibernate with Spring transaction management in my application. There is a InspectionEntity which has a one-to-many relation with InspectionDetailEntity. The PKs for both the entity's are created by an insert trigger in the database. The PK values when I try to save is the default 0 value. I add the InspectionDetailEntity to the InspectionEntity by calling addInspectionDetail() and then save the InspectionEntity - <code>entityManager.persist(inspection);</code> What I expect is the Inspection to be saved with all the InspectionDetail's. But this results in a <strong>org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.xyz.entity.InspectionDetailEntity#0].</strong> I also tried setting the Set directly into the InspectionEntity (without using the addInspectionDetail() ). This also results in the same error. Any help would be highly appreciated.</p> <pre><code>@Entity @Table(name = "tb_inspection") public class InspectionEntity { @Id @Column(name = "inspection_id") private Integer inspectionId; @OneToMany(fetch = FetchType.EAGER, mappedBy = "inspectionDetailId", cascade = CascadeType.ALL) @JoinColumn(name = "inspection_id", insertable = true, updatable = true) private Set&lt;InspectionDetailEntity&gt; inspectionDetails; /** * Adds a inspectionDetailEntity to the set of inspectionDetails * @param inspectionDetailEntity */ public void addInspectionDetail(InspectionDetailEntity inspectionDetailEntity) { if(inspectionDetails == null) inspectionDetails = new HashSet&lt;InspectionDetailEntity&gt;(); inspectionDetailEntity.setInspection(this); inspectionDetails.add(inspectionDetailEntity); } //setter and getters } @Entity @Table(name = "tb_inspection_dtl") public class InspectionDetailEntity { @Id @Column(name = "inspection_dtl_id") private Integer inspectionDetailId; @Column(name = "inspection_id") private Integer inspectionId; @ManyToOne @JoinColumn(name="inspection_id", insertable = false, updatable = false) private InspectionEntity inspection; //setters and getters } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload