persistence@glassfish.java.net

Re: Unidirectional OneToMany

From: Marina Vatkina <Marina.Vatkina_at_Sun.COM>
Date: Mon, 06 Nov 2006 12:38:22 -0800

Hi Antonio,

One note below (I think it was a typo on Markus's part).

Markus Fuchs wrote On 11/03/06 13:23,:
> Hi Antonio,
>
> According to the JPA spec, unidirectional OneToMany relationships should
> be mapped to a join table (see 2.1.8.5.1 Unidirectional OneToMany
> Relationships). The issue here is that the Collection side owns the
> relationship and is responsible for writing relationship updates to the
> database.
>
> Unidirectional OneToMany relationships could be provided as vendor
> extension, and are not portable.

Unidirectional OneToMany relationships *mapped to a foreign key* could be
provided as vendor extension, and are not portable.

-marina

>
> Only bi-directional relationships need a mappedBy annotation. The
> mappedBy annotation identifies the owning side. For a unidirectional
> relationship, there's only one side, therefore no need to identify an
> owning side.
>
> -- markus.
>
> Antonio Goncalves wrote:
>
>> Hoops, sorry. The first example without annotation (private
>> List<OrderLine> orderLines) produces a jointable called
>> Order_OrderLine. The question is more how to not have a join table but
>> a foreign key in OrderLine ?
>>
>> 2006/11/3, Antonio Goncalves <antonio.mailing_at_gmail.com
>> <mailto:antonio.mailing_at_gmail.com>>:
>>
>> Hi.
>>
>> I'm using GlassFish V2b24. I want to model a unidirectional
>> OneToMany relationship between the two classes Order -----*->
>> OrderLine. A bi-directional relation works fine (addind a Order
>> attribut into OrderLine), but I can't make the unidirectional
>> work. Here is the code (simplified) :
>>
>>
>> public class *Order* implements Serializable {
>> @Id @GeneratedValue
>> private Long id;
>> @ManyToOne @JoinColumn(name = "customer_fk", nullable = false)
>> private Customer customer;
>> * private List<OrderLine> orderLines;
>> *}
>>
>> public class *OrderLine* implements Serializable {
>> @Id @GeneratedValue
>> private Long id;
>> private Integer quantity;
>> @OneToOne(optional = false) @JoinColumn(name = "item_fk")
>> private Item item;
>> }
>>
>> That works at deployment and creates both tables but without any
>> foreign key or a join table between them. When I add the
>> @OneToMany annotation :
>>
>> *_at_OneToMany
>> private List<OrderLine> orderLines;*
>>
>> It produces the following error at deployment :
>>
>> org.apache.derby.client.am.SqlException: 'ORDER_FK' is not a
>> column in table or VTI 'DBUSER.T_ORDER_LINE'.
>>
>> And I would love to have a foreign key order_fk. I've also read
>> <http://www.oracle.com/technology/products/ias/toplink/jpa/resources/toplink-jpa-annotations.html#OneToMany>
>> that for a unidirectional I need to use the mappedBy attribute.
>> But this is what happens
>>
>> @OneToMany(*mappedBy = "order"*)
>> private List<OrderLine> orderLines;
>>
>> Exception Description: The attribute [orderLines] in entity class
>> [class com.yaps.petstore.entity.order.Order] has a mappedBy value
>> of [*order*] which does not exist in its owning entity class
>> [class com.yaps.petstore.entity.order.OrderLine ]. If the owning
>> entity class is a @MappedSuperclass, this is invalid, and your
>> attribute should reference the correct subclass.
>>
>> What am I doing wrong ? Does anybody have an idea ?
>>
>> Thanks,
>>
>> Antonio
>>
>>