I have following object structure:
An abstract class InvolvedObject with subclasses InvolvedParty and InvolvedItem. The idea is to have a one-to-one parent relation form InvolvedObject to InvolvedObject and also a one-to-many relation to the children of type InvolvedObject (so self referencing, bi-directional). The inheritance strategy is found best to be JOINED (one 'shared' super table and one table per subclass). (Non relevant class attributes are not shown)
Mapped with JPA annotations as followed:
@Entity @Table(name = "INVOLVED_OBJECT") @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(name = "DISCRIMINATOR", discriminatorType = DiscriminatorType.STRING ) public abstract class InvolvedObject{
/** The relation referencing to another involved object (parent) */ private InvolvedObject involvedObjectRelation;
/** The relations to other involved objects */ private List <InvolvedObject>involvedObjectRelations = new ArrayList<InvolvedObject>();
/** * Returns the involved objects to which this object has a relation * * @return The involved objects to which this party has a relation * @hibernate.bag lazy="true" cascade="all-delete-orphan" inverse="true" * @hibernate.collection-key column="RELATION_ID" * @hibernate.collection-one-to-many class="com.gudrun.gdp3000.business.involvedobject.domain.InvolvedObject" */ @OneToMany @LazyCollection(LazyCollectionOption.TRUE) @Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN}) @JoinColumn(name="RELATION_ID") public List<InvolvedObject> getInvolvedObjectRelations() { return involvedObjectRelations; }
/** * * Returns the related involved object * * @return The related involved object * @hibernate.many-to-one column="RELATION_ID" not-null="false" */ @ManyToOne @JoinColumn(name="RELATION_ID") public InvolvedObject getInvolvedObjectRelation() { return involvedObjectRelation; } // and getters of course }
Now, when I try to copy an InvolvedParty object (with its relations), I get a 'Found shared references to a collection'. In the hibernate source, I saw that only silly users should get this kind of error (nice nice) since it indicates eg. a circular reference. But... we did this kind of mapping before (what I was doing was actually refactoring some part of the code and used more or less the mapping config from the other example... the only difference is a single table compared to a joined table inheritance strategy)
So, following test case didn't work (with previously mentioned error).
public void testCopyInvolvedParty() { for (Iterator<InvolvedParty> iterator = originalParties.iterator(); iterator.hasNext();) { final InvolvedParty originalInvolvedParty = ((InvolvedParty) iterator.next()); InvolvedParty copiedParty = (InvolvedParty) originalInvolvedParty.valueCopy(); // the value copy only copies class attributes, no references / lists involvedObjectFacade.createInvolvedParty(copiedParty); List<InvolvedObject> involvedObjectRelations = originalInvolvedParty.getInvolvedObjectRelations(); if (involvedObjectRelations != null && involvedObjectRelations.size() > 0) { for (InvolvedObject involvedObject : involvedObjectRelations) { InvolvedObject copiedInvolvedObject = involvedObject.valueCopy(); copiedParty.addInvolvedObjectRelation(copiedInvolvedObject); } involvedObjectFacade.updateInvolvedObject(copiedParty); } } }
Btw, the error happens when there is more than one child. With one child the code is ok.
(I can understand that more detail might be needed, so please ask and I will try to give you it).
Sincerely, Dieter.
|