Hibernate - NonUniqueObjectException
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 - entityManager.persist(inspection);
What I expect is the Inspection to be saved with all the InspectionDetail's.
But this results in a
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.xyz.entity.InspectionDetailEntity#0].
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.
@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<InspectionDetailEntity> inspectionDetails;
/**
* Adds a inspectionDetailEntity to the set of inspectionDetails
* @param inspectionDetailEntity
*/
public void addInspectionDetail(InspectionDetailEntity inspectionDetailEntity) {
if(inspectionDetails == null)
inspectionDetails = new Has开发者_JAVA技巧hSet<InspectionDetailEntity>();
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
}
Don't set id to 0 before you save it. Leave it as null. If value is set Hibernate will assume that object is already in the database and attempt to update it.
And you probably ending up with multiple objects with the same id (0).
Update
As I think about it, you are, probably, forced to set id to some value because you don't have id generator and Hibernate expects you to provide value for id. However, there is nothing in Hibernate that would update ids on your objects after insert. So you end up with multiple objects in session with id 0.
I am not sure what would be good solution for this situation. Most likely you will need to write custom id generator. But in case of sequences, for example, it requires extra call to the database. May be you should consider id assignment other than trigger?
Try removing either
inspectionDetailEntity.setInspection(this);
or
inspectionDetails.add(inspectionDetailEntity);
I don't think you need both.
It might be worth considering a unidirection relationship between your child and parent
精彩评论