• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

JPA - foreign key filed question

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have recently started my adventure with JPA (2.0) and i have a question about foreign keycolumn which is defined in anotation for relation @JoinColumn(name="foreign_key").
For example let's take OneToOne biderectional relation for two entities Order and Invoice where Invoice is an "owning" side. So the foregin key column is in Invoice table in data base with name Order_Id. The Order_Id is a primary key (int) in Order entity.
My relation from Invoice side would look like thid
@Entity
publi class Invoice {

....

@OneToOne
@JoinColumn(name ="Order_Id" )
public Order getOrder() {
return order;
}

private Order order;

...
// private int Order_Id; // should it be defined in this entity?

}
The question is whether this entity (Invoice) should possess definded field (or property) Order_Id which would be mapped on Invoice table foreign key column?
I ask about it because I have seen both cases in examples (with such field and without it) and i suppose that one of them must be wrong.

 
Ranch Hand
Posts: 247
Eclipse IDE Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

private Order order;



Defining like this is correct one... Since all are done in Object Oriented manner... Invoice "has"(possess) Order object... JPA Entity Manager will deal the foreign key mapping between Invoice and Order with the help of annotation "JoinColumn"... Thats the purpose JoinColumn annotation serves here...

Defining directly the "Order_id" int type means manually we have to give the Order id by not utilizing the Entity Manager Auto-Mapping feature...

When persisting the new Invoice object, the new Invoice object should have "Order" object set in the new Invoice object..

While making an insertion of new Invoice object as relational record in relational database, implicitly Entity Manager search for "Order_Id" DB field name (Order Table Primary Key field) mapped instance variable in Order object and maps to foreign key column "Order_Id" of Invoice table...

like..

insert into Invoice(col1,col2,Order_Id) values(val_col1,val_col2,<Order_Id_value extracted from Order object which was possessed by Invoice Object>)
 
Witold Marshal
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks : )
 
reply
    Bookmark Topic Watch Topic
  • New Topic