The workaround is to use MappedById annotation (JPA 2.0 spec) in Openjpa trunk:
@Entity
public class Child implements Serializable {
@EmbeddedId
@Column(name = "id", unique = false, nullable = false)
private ChildId childId;
@MappedById("parent")
@ManyToOne
@JoinColumn(name = "parent_id", referencedColumnName = "id")
private Parent parent;
...
}
@Embeddable
public class ChildId {
public long id;
public long parent;
...}
@Entity
public class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, insertable = false, updatable = false,
nullable = false)
private long id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "parent")
private List<Child> children = new ArrayList<Child>();
private String name;
...}
For more detail, please see
org.apache.openjpa.persistence.enhance.identity.TestMappedById.testMappedById5
-Fay
--- On Mon, 3/30/09, Nemanja Joksovic <[email protected]> wrote:
> From: Nemanja Joksovic <[email protected]>
> Subject: Re: Identity class and parent/children entity relationship
> To: [email protected]
> Date: Monday, March 30, 2009, 9:56 AM
> Thank you very much.
>
> Nemanja J.
>
>
>
> The second problem is a known problem. I will open a JIRA
> for it.
>
>
> --- On Sun, 3/29/09, Nemanja Joksovic
> <[email protected]> wrote:
>
> > From: Nemanja Joksovic <[email protected]>
> > Subject: Identity class and parent/children entity
> relationship
> > To: [email protected]
> > Date: Sunday, March 29, 2009, 5:33 AM
> > Hi all,
> >
> > I've been experiencing few problems with Identity
> class
> > and Parent/Children entity relationship. I tried with
> both
> > OpenJPA 1.2.1 and 2.0.0-SNAPSHOT. I have a very simple
> test
> > case which can briefly explain problems:
> >
> > @Entity
> > public class Parent implements Serializable {
> >
> > private long id;
> > ....
> > private List<Child> children = new
> > LinkedList<Child>();
> >
> > @Id
> > // @GeneratedValue(strategy =
> GenerationType.IDENTITY)
> > @Column(name = "id", unique = true,
> > insertable = false, updatable = false, nullable =
> false)
> > public long getId() {
> > return id;
> > }
> >
> > public void setId(long id) {
> > this.id = id;
> > }
> >
> > ...
> >
> > @OneToMany(cascade = CascadeType.ALL, mappedBy =
> > "parent")
> > public List<Child> getChildren() {
> > return children;
> > }
> >
> > public void addChild(Child child) {
> > if (child == null) {
> > throw new
> IllegalArgumentException("Cannot
> > add a null Child");
> > }
> > this.getChildren().add(child);
> > }
> >
> > public void setChildren(List<Child>
> children) {
> > this.children = children;
> > }
> > }
> >
> > @Entity
> > @IdClass(ChildId.class)
> > public class Child implements Serializable {
> >
> > private long id;
> > ...
> > private Parent parent;
> >
> > @Id
> > @Column(name = "id", unique = false,
> nullable
> > = false)
> > public long getId() {
> > return id;
> > }
> >
> > public void setId(long id) {
> > this.id = id;
> > }
> >
> > ...
> >
> > @Id
> > @ManyToOne
> > @JoinColumn(name = "parent_id",
> > referencedColumnName = "id")
> > public Parent getParent() {
> > return parent;
> > }
> >
> > public void setParent(Parent parent) {
> > this.parent = parent;
> > }
> > }
> >
> > ChidId is generated with the Application Identity
> Tool:
> >
> > public class ChildId implements Serializable {
> > ...
> > public long id;
> > public long parent;
> > ...
> > }
> >
> > 1) Merge operation fail in any case with following
> > exception:
> >
> > java.lang.NullPointerException
> > at
> >
> net.company.persistence.Child.pcCopyKeyFieldsFromObjectId(Child.java)
> > at
> >
> net.company.persistence.Child.pcNewInstance(Child.java)
> > at
> >
> org.apache.openjpa.kernel.AttachStrategy.persist(AttachStrategy.java:93)
> > at
> >
> org.apache.openjpa.kernel.VersionAttachStrategy.attach(VersionAttachStrategy.java:100)
> > at
> >
> org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:241)
> > at
> >
> org.apache.openjpa.kernel.AttachStrategy.attachCollection(AttachStrategy.java:333)
> > at
> >
> org.apache.openjpa.kernel.AttachStrategy.replaceList(AttachStrategy.java:359)
> > at
> >
> org.apache.openjpa.kernel.AttachStrategy.attachField(AttachStrategy.java:223)
> > at
> >
> org.apache.openjpa.kernel.VersionAttachStrategy.attach(VersionAttachStrategy.java:153)
> > at
> >
> org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:241)
> > at
> >
> org.apache.openjpa.kernel.AttachManager.attach(AttachManager.java:101)
> > at
> >
> org.apache.openjpa.kernel.BrokerImpl.attach(BrokerImpl.java:3212)
> > ... 25 more
> >
> >
> > The problem is caused by the call
> > pc.NewInstance(AttachStrategy:93) with null state
> manager
> > instance which is used in
> pcCopyKeyFieldsFromObjectId(..):
> >
> >
> pcsetParent((Parent)pcStateManager.getPCPrimaryKey(childid,
> > 2 + pcInheritedFieldCount));
> >
> > Also, for same problem exists JIRA Issue:
> > https://issues.apache.org/jira/browse/OPENJPA-218
> >
> >
> > 2) Persist working fine, but it's fail in the case
> when
> > Parent.id is Generated Value field with following
> exception:
> >
> > <openjpa-1.2.1-r752877:753278 fatal user error>
> > org.apache.openjpa.persistence.InvalidStateException:
> > Detected reentrant flush. Make sure your flush-time
> > instance callback methods or event listeners do not
> invoke
> > any operations that require the in-progress flush to
> > complete.
> > at
> >
> org.apache.openjpa.kernel.BrokerImpl.flushSafe(BrokerImpl.java:1904)
> > at
> >
> org.apache.openjpa.kernel.BrokerImpl.flush(BrokerImpl.java:1679)
> > at
> >
> org.apache.openjpa.kernel.StateManagerImpl.assignObjectId(StateManagerImpl.java:524)
> > at
> >
> org.apache.openjpa.kernel.StateManagerImpl.assignObjectId(StateManagerImpl.java:506)
> > at
> >
> org.apache.openjpa.kernel.StateManagerImpl.fetchObjectId(StateManagerImpl.java:1434)
> > at
> >
> net.company.persistence.Parent.pcFetchObjectId(Parent.java)
> > at
> >
> net.company.persistence.Child.pcCopyKeyFieldsToObjectId(Child.java)
> >
> >
> > Does exists some workaround for these problems ?
> >
> > Regards,
> > Nemanja J.
> > --
> > View this message in context:
> >
> http://n2.nabble.com/Identity-class-and-parent-children-entity-relationship-tp2552430p2552430.html
> > Sent from the OpenJPA Users mailing list archive at
> > Nabble.com.
>
>
>
>
>
>
> --
> View this message in context:
> http://n2.nabble.com/Identity-class-and-parent-children-entity-relationship-tp2552430p2558373.html
> Sent from the OpenJPA Users mailing list archive at
> Nabble.com.