• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

EJB 3 in Action - JPA Doubt

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 329
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply. How about OneToOne mapping in the above case. I mean OneToOne association between Employee to Car.

Regards t
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic