openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Fay Wang (JIRA)" <j...@apache.org>
Subject [jira] Commented: (OPENJPA-1253) JoinColumn annotation not allowed in conjunction with *ToMany annotation
Date Thu, 17 Sep 2009 17:11:57 GMT

    [ https://issues.apache.org/jira/browse/OPENJPA-1253?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=12756626#action_12756626
] 

Fay Wang commented on OPENJPA-1253:
-----------------------------------

JPA 2.0 supports non-default mapping (sec 2.9): 

"In addition, this specification also requires support for the following alternative mapping
strategies: the mapping of unidirectional one-to-many relationships by means of foreign key
mappings; the mapping of unidirectional and bidirectional one-to-one relationships, bidirectional
many-to-one/one-to-many relationships, and unidirectional many-to-one relationships by means
of join table mappings. The JoinColumn and JoinTable annotations or corresponding XML elements
must be used to specify such non-default mappings. See sections 11.1.21 and 11.1.23."

For *toMany relation, the non-default and default mappings, according to the spec, are:

(1) uni-/OneToMany/foreign key strategy 
(2) uni-/OneToMany/join table strategy (default)
(3) bi-/OneToMany/foreign key strategy (default)
(4) bi-/OneToMany/join table strategy 
(5) uni-/ManyToMany/join table strategy (default)
(6) bi-/ManyToMany/join table strategy (default)

For (1), the spec provides the following example (Sec 11.1.36):
Example 3: Unidirectional One-to-Many association using a foreign key mapping
In Customer class:
@OneToMany(orphanRemoval=true)
@JoinColumn(name="CUST_ID") // join column is in table for Order
public Set<Order> getOrders() {return orders;}

For (4), Bi-directional One-to-Many association using the join table mapping
In Customer class:
@OneToMany(mappedBy="customer")
    @JoinTable(
        name="Customer_Orders",
        joinColumns=
          @JoinColumn(name="Order_ID", referencedColumnName="ID"),
         inverseJoinColumns=
          @JoinColumn(name="Cust_ID", referencedColumnName="ID")
    )

public Set<Order> getOrders() {return orders;}

Note that in this scenario, @JoinTable is required. Simply applying @JoinColumn without @JoinTable
will result in an exception thrown by openjpa.

Similarly for ManyToMany mapping, @JoinTable is required as shown by the example in the spec
(11.1.25). Simply applying @JoinColumn will fail with an exception:
@ManyToMany
@JoinTable(
    name="CUST_PHONE",
    joinColumns=
       @JoinColumn(name="CUST_ID", referencedColumnName="ID"),
    inverseJoinColumns= 
       @JoinColumn(name="PHONE_ID", referencedColumnName="ID")
)
public Set<PhoneNumber> getPhones() { return phones; }

The failing example provided in this JIRA is not valid:
    @MapKey(name = "basic")
    @OneToMany(mappedBy = "oneManyOwner")
    @JoinColumn(name = "asdf")
    protected Map<String, AnnoTest2> inverseOwnerMapKey = new HashMap();

This is a bi-directional OneToMany assocation. If the default foreign key strategy is intended,
no JoinColumn annotation should be applied. If the non-default join table strategy is intened,
the JoinTable annotation should be applied.














> JoinColumn annotation not allowed in conjunction with *ToMany annotation
> ------------------------------------------------------------------------
>
>                 Key: OPENJPA-1253
>                 URL: https://issues.apache.org/jira/browse/OPENJPA-1253
>             Project: OpenJPA
>          Issue Type: Bug
>          Components: jdbc
>    Affects Versions: 2.0.0-M1, 2.0.0-M2, 2.0.0-M3, 2.0.0-M4, 2.0.0
>            Reporter: Rick Curtis
>             Fix For: 2.0.0
>
>
> I receive the following exception if I have an Entity with a One(Many)ToMany relationship
with another Entity. 
> <openjpa-2.0.0-SNAPSHOT-r422266:805588 fatal user error> org.apache.openjpa.persistence.ArgumentException:
You have supplied columns for "....Entity", but this mapping cannot have columns in this context.
> To recreate the failure, add a @JoinColumn annotation to one of the *ToMany relationships
in org.apache.openjpa.persistence.jdbc.annotations.AnnoTest1. 
> example:
>     @MapKey(name = "basic")
>     @OneToMany(mappedBy = "oneManyOwner")
>     @JoinColumn(name = "asdf")
>     protected Map<String, AnnoTest2> inverseOwnerMapKey = new HashMap();
> Then run:
> trunk\openjpa-parent\openjpa-persistence-jdbc>mvn test -Dtest=org.apache.openjpa.persistence.jdbc.annotations.TestOneToMany

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


Mime
View raw message