• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

EJB 3 in Action - JPA Doubt  RSS feed

 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
  • 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
  • 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
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!