• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

Is @JoinColumn required in one to many bidirectional relationship?

 
Bartender
Posts: 2085
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In chapter 5 of JPA Pro2 , there is an example called oneToManyList,
it tries to demonstrate the one to many and many to one relationship between employees and a department.

In the Department.java :

In Employee.java



There are two tables in the DB:
Employee : ID, NAME , SALARY, DEPARTMENT_ID where ID is the primary key, Department_ID is the foreign key.
Department: ID , Name, where ID is the primary key.


I think in Employee.java, the @JoinColumn is missing even though it still works.
So, I added this to Employee.java:

 
Creator of Enthuware JWS+ V6
Posts: 3392
312
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, @JoinColumn is only needed when the default mapping does not apply. In your case the foreign key column matches the default mapping and that is why you can leave it out.

2.10.2 Bidirectional ManyToOne / OneToMany Relationships
Assuming that:

   Entity A references a single instance of Entity B.
   Entity B references a collection of Entity A.

Entity A must be the owner of the relationship.

The following mapping defaults apply:
Entity A is mapped to a table named A.
Entity B is mapped to a table named B.
Table A contains a foreign key to table B. The foreign key column name is formed as the concatenation of the following: the name of the relationship property or field of entity A;  "_"; the name of the primary key column in table B. The foreign key column has the same type as the primary key of table B.

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic