persistence@glassfish.java.net

Re: Unidirectional OneToMany

From: Antonio Goncalves <antonio.mailing_at_gmail.com>
Date: Sat, 4 Nov 2006 09:44:38 +0100

Thanks for this precision Markus.

2006/11/3, Markus Fuchs <Markus.Fuchs_at_sun.com>:
>
> 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.
>
> 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>:
> >
> > 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
> >
>
>