wood burning stoves*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes EJB 3 in Action  - JPA Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "EJB 3 in Action  - JPA Doubt" Watch "EJB 3 in Action  - JPA Doubt" New topic
Author

EJB 3 in Action - JPA Doubt

nitin pai
Ranch Hand

Joined: May 30, 2006
Posts: 185
I am not able to understand this statement from the book "EJB3 in Action" -
Page 280, which says:


JPA does not support unidirectional one to many relationships using a foreign key on the target table and you cannot use the following mapping if you have a unidirectional one to many relationship between Item and Bid:

@OneToMany(cascase=CascadeType.ALL)
@JoinColumn(name="ITEM_ID", referencedColumnNames="BID_ITEM_ID")
preotected Set<Bid> bids;


Can you tell what it means?



bernard savary
Ranch Hand

Joined: Jan 09, 2008
Posts: 91
Hi ,
Unidirectional OneToMany relationships are not directly supported by JPA however they can be done by the use of a @JoinTable (similar to ManyToMany)
I think its best you read this part from the specs .
Sergio Tridente
Ranch Hand

Joined: Mar 22, 2007
Posts: 329

Thank you. This was pretty enlightening for me as there's no such clarification in O'Reilly's book.

For the ones that might be interested, I am copying the relevant extract from the specs (section 9.1.24 OneToMany Annotation):


The default schema-level mapping for unidirectional one-to-many relationships uses a join table, as described in Section 2.1.8.5. Unidirectional one-to-many relationships may be implemented using one-to-many foreign key mappings, however, such support is not required in this release. Applications that want to use a foreign key mapping strategy for one-to-many relationships should make these relationships bidirectional to ensure portability.


SCJP 1.4 (88%) - SCJP 5.0 Upgrade (93%) - SCWCD 1.4 (97%) - SCBCD 5.0 (98%)
Thiva Kugan
Greenhorn

Joined: Nov 20, 2007
Posts: 3
Hi Guys,

I am new to JPA and i have a question about the OneTOMany unidirectional mapping. Let we assume the following example two entities
Person (ID, NAME) and Car (ID, CAR_NAME, PER_ID) i.e the foreign key is in the target table.

Here is my relationship mapping inside the Person entity;

@OneToMany(cascade = CascadeType.ALL)
@JoinTable(name = "CAR",
joinColumns = @JoinColumn(name = "PER_ID"),
inverseJoinColumns = @JoinColumn(name = "ID"))
private List<Car> cars;

Note this is an unidirectional OneToMany mapping from Person to Car. I am using Eclipselink and the above mapping is always not working . Sometimes system throws the exception and says CAR_ID is not found.

If any of you came similar issue please share your idea. Much appreciate your reply.

Regards t
Jun Liu
Greenhorn

Joined: Nov 15, 2005
Posts: 11
As said above, unidirectional OneToMany can not use foreign key mapping in current JPA. Instead a join table should be used. So the tables should be Person(ID, NAME), Car(ID, CAR_NAME) and Person_Car(PERSON_ID, CAR_ID).
Thiva Kugan
Greenhorn

Joined: Nov 20, 2007
Posts: 3
Thanks for the reply. How about OneToOne mapping in the above case. I mean OneToOne association between Employee to Car.

Regards t
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EJB 3 in Action - JPA Doubt