I've the following model : An Organization has a List<Contract>, each Contract may have a pricebookId and a collection of PricebookEntry. Multiple Contract entities can have the same pricebookId. The Collection<PricebookEntry> is the result of a @OneToMany association with the pricebookId as JoinColumn.
Here is the postulate: An organization has 2 contracts and both contracts have no pricebookId.
And here is my problem: During an Hibernate session where this particular Organization is involved, Hibernate throws this exception:
org.hibernate.HibernateException: Found shared references to a collection: Contract.pricebookEntries.
Indeed, both contracts have their pricebookEntries set to the same PersistentBag (same reference).
How can I fix this ?
Thank you for your help!
@Entity
public class Organization implements Serializable {
@Id
private String id;
@Fetch(FetchMode.SUBSELECT)
@OneToMany(fetch = FetchType.EAGER, mappedBy = "organization")
private final List<Contract> contracts = new ArrayList<>();
}
@Entity
public class Contract implements Serializable {
@Id
private String id;
@Column(name = "pricebook_id")
private String pricebookId;
@JoinColumn(name = "organization_id", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private Organization organization;
@JoinColumn(name="pricebookId", referencedColumnName = "pricebook_id")
@OneToMany(fetch = FetchType.EAGER)
@Fetch(FetchMode.SUBSELECT)
private final Collection<PricebookEntry> pricebookEntries = new ArrayList<>();
}
@Entity
public class PricebookEntry {
@Id
private String id;
private String pricebookId;
}