File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Object Relational Mapping and the fly likes Associations in Hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Associations in Hibernate" Watch "Associations in Hibernate" New topic

Associations in Hibernate

Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143

I am new to Hibernate and I am reading the Hibernate in Action book.
I am unable to understand Associations.
We make an association bidirectional by setting inverse=true in the mapping file.
The book example says there is a bidirectional relationship between Item and Bid.
But in the table relationship diagram there is a primary key BID_ID in the BID table and a foreign key ITEM_ID which shows the association between Bid and item.
The other association (ie the other way round ) between Item and BId, there is no foreign key in ITEM table.
So just saying inverse=true, would that make it a bidirectional association .
Don't we have to have a foreign key in the ITem table ?

If anyone could please explain me this or redirect me to an URL etc which could explain associations.

Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
The bi-directional association you specify with Hibernate only exists in the Java application, not in the database. In other words, the inverse="true" you type into your configuration file means that
1) Hibernate fetches the "parent" object and the "child" object(s),
2) Hibernate says "hey, parent, here's your children", and then
3) Hibernate says "hey, children, here's your parent"

Even though the foreign key technically points from one table to another, and not the other way around, semantically that foreign key is a two-way relationship. Hibernate doesn't restrict itself on the technical but instead (and rightly so) handles things on the semantic level.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Gayatri Ganesh
Ranch Hand

Joined: Dec 23, 2003
Posts: 143
Thanks Lasse.
I agree. Here's the link:
subject: Associations in Hibernate
It's not a secret anymore!