Hi Marc,

Did some more reading and I was wrong about the use case for the @Element
annotations. The @Element annotations allow you to specify the FK constraint
on the One side of a @OneToMany, so your annotations would look like this :

@Entity
public class Father extends Person {
    @OneToMany
    @ElementForeignKey
    @ElementJoinColumn(name="father_oid", referencedColumnName="oid")
    Collection<Child> children;
. . .
}

@Entity
public class Child extends Person {
    @ManyToOne
    private Father father;
. . .
}

It appears to be functionally identical to using @JoinColumn and @ForeignKey
on the Child class, but the documentation is a bit sparse on this
annotation..

There are several examples in our unit tests though. For example
openjpa-persistence-jdbc/src/test/java/org/apache/openjpa/persistence/jdbc/mapping/bidi/ParentWithAutoIdentity.java
looks somewhat similar to your model.

Hope this helps,
-mike


---------- Forwarded message ----------
From: Michael Dick <[email protected]>
Date: Fri, Nov 13, 2009 at 9:52 AM
Subject: Re: inverse OneToMany relation - getting weird
To: [email protected]


Hi Marc,

I think the @ElementForeignKey and @ElementJoinColumn annotations are
intended to be used with non-entity types (ie PersistentCollections).

If Child is an entity you'd want to use @ForeignKey and @JoinColumn instead
(I gave this a quick try and it looks like it worked for me).

Regards,
-mike


On Fri, Nov 13, 2009 at 7:03 AM, Marc Logemann <[email protected]> wrote:

> Hi,
>
> after struggling for several hours, i need to ask for help.
>
> Following scenario.....
>
> DB Table: Father
> ---------------------------
> INT oid
> ....
>
>
> DB Table: Childs
> ------------------------
> INT oid
> INT father_oid
> ...
>
> Java Entity: Father
> --------------------------
> ...
> @OneToMany(cascade = CascadeType.PERSIST, mappedBy = "father")
>  private List<Child> childList;
>
> Java Entity: Child
> --------------------------
> ...
> @ManyToOne(cascade = CascadeType.PERSIST)
> @ElementForeignKey
> @ElementJoinColumn(name="father_oid", referencedColumnName="oid")
> protected Father father;
>
>
> When i try to persist a Father, i am getting:
>
> <openjpa-1.2.0-r422266:683325 fatal user error>
> org.apache.openjpa.persistence.ArgumentException: Field "Father.childList"
> declares "Child.father" as its mapped-by field, but this field is not a
> direct relation. at......
>
> Ok, then i changed Child to:
>
> Java Entity: Child
> --------------------------
> ...
> @ManyToOne(cascade = CascadeType.PERSIST)
> @Column(name = "father_oid")
> protected Father father;
>
>
> When i now do persist a Father object, both DB records (Father and Childs)
> are filled with records but the FK column in Child (father_oid) is empty. So
> the FK relation is broken but it did persist childs, which is somehow weird.
>
> So whatever i do, i never get a fully persisted graph with correct FK
> values.
>
> Can anyone give me a hint what i am doing wrong here?
>
>
> Thanks a lot.
>
> ---
> regards
> Marc Logemann
> http://www.logemann.org
> http://www.logentis.de
>
>
>
>
>

Reply via email to