• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Is @JoinColumn required in one to many bidirectional relationship?

 
Ranch Hand
Posts: 1748
12
  • Mark post as helpful
  • send pies
  • 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: 3292
296
Android Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • 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.

 
Good night. Drive safely. Here's a tiny ad for the road:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!