Thursday, February 15, 2007

Hibernate: When is "inverse=true" and when it's not?

When I was a hibernate beginner, I was confused about deciding "inverse=true" or "inverse=false".

Here is an easy way to understanding it:

example scenario:
Person(one) <-> Address(many)
* bi-directional one-to-many relationship. (A person has multiple addresses.)

public class Person {
private Integer id;
private Set<Address> addresses;

// setter, getter
Set<Address> getAddresses() { return addresses; }

public class Address {
private Integer id;
private Person person;

// setter, getter
Person getPerson() { return person; }
  • Person class has "Set<Address> getAddresses()" method
  • Address class has "Person getPerson()" method

If you think about the relation between two classes, it may gives an idea that person has addresses. (Person -> Addresses)
So, it feels like a person is an owner, and an address is a child. Then, you want to think that address is "inverse=true" because address is owned by person.
However, it's not correct.

Here, I'd like to suggest a way to think about it.

Let's look at table structure instead of classes.
  • PERSON[ id, name, ...]
  • ADDRESS[ id, person_id, city, street,...]

The person_id column in Address table is the relational information between thease two tables.
So, Address is an owner of the relationship, and Person is the inverse side.
"inverse=true" means "this side is the inverse side", and "inverse=false" means "this is not the inverse side. this side is the owner of the relationship".

Answer is:

<class name="Person">
<id name="id">...</id>
<set name="addresses" inverse="true">
<key column="person_id"/>
<one-to-many class="Address"/>

<class name="Address">
<id name="id">...</id>
<many-to-one name="person" class="Person" />

In sum, look at the table structure to distinguish "inverse=true" or "inverse=false".
If the table has relational information, then it is the owner side. So, it's not inverse side.(inverse=false)
If the table doesn't have relational information, then it is the inverse side. So, it needs "inverse=true".


Anonymous said...

Another way to look at it is by looking at the many end. In such cases the inverse=true goes on the end with the collection.

Anonymous said...

It's much simpler: If you map the same foreign key column twice, you need to "disable" one side by making it inverse. Which side varies.

If I have a one-to-many with a persistent List or Map, I'd make that side inverse="false" and the many-to-one should be disabled for updating/insertion with insert="false" and update="false".

If I have one-to-many with a persistent Set, I'd make that side inverse="true" and let the many-to-one update/insert the foreign key column.

If I have a many-to-many with a persistent Set on both sides, I can pick which side is "disabled", no difference.

If I have a many-to-many with a persistent List or Map on one of the sides, I need to enable that side and disable the other side (otherwise the index/map key of that list or map isn't stored.)

That's it.

LeX said...

Firstly, thanks for the great explanation. I have some question regarding one of the post though..I was hoping you would be able to explain it to me.

"If I have a one-to-many with a persistent List or Map, I'd make that side inverse="false" and the many-to-one should be disabled for updating/insertion with insert="false" and update="false"."

Why did he treat the "inverse" differently for "Set" & "List/Map" ?

Thanks again.

Pratibha said...

Thanks for great explanation.i have one query
Person aPerson = (Person) session3.load(Person.class, personId);

Where events is a set
But i am not able to remove the corrosponding row,i can only remove row using Session.delete(aPerson)

Any Comments would be highly appreciated


aiyipianni said...

Aston Villa rode their luck at Hull City where an 88-minute own goal from Kamil Zayatte saw them leapfrog three points clear of Arsenal and into fourth place in the Premier League wow gold with a 1-0 win.

Villa had to survive Hull penalty wotlk gold appeals for a handball against Ashley Young in time added on, television replays showing that referee Steve Bennett wow gold correctly rejected the claims after consulting a linesman.

Bennett had been involved in controversy after just five minutes when American goalkeeper Brad Friedel looked to have handed Hull the initiative and threaten Villa's return to the Champions League qualifying wow gold zone.

Friedel spilled wow gold the ball under pressure from Nick Barmby and stand-in right-back Nigel Reo-Coker turned it into his own net as he attempted to wow gold clear.

But Bennett cut short wow gold celebrations at the KC Stadium -- and let Friedel off the hook -- when he ruled out the score for an wow wotlkapparent infringement by Barmby.

Zayatte's intervention from a Young cross bound for wow gold Gabriel Agbonlahor then saw Villa leapfrog Arsenal and draw level with Manchester United on 38 points -- seven adrift of leaders Liverpool and four wow gold behind Chelsea.

Stung by an on-pitch dressing down wow gold by manager Phil Brown at Manchester City last week, Hull showed five changes and a vastly improved performance.

Promoted Hull were looking for only their second win in 11 games while wow gold Villa arrived unbeaten in seven and it looked to be heading for a goalless draw when the home side suffered a cruel late blow.

aiyipianni said...

Dimitar Berbatov buy flyff gold scored the only goal as Manchester United beat Final Fantasy XI gil Middlesbrough 1-0 at Old Trafford on Monday buy cheap ffxi gil to move to within seven points of Premier League ffxi gil leaders Liverpool -- with two vital buy Warhammer gold games in hand.

The Bulgarian Warhammer gold international striker fired home a volley from close-range in the buy Warhammer gold 69th minute to give United the victory, leaving Middlesbrough deep in relegation trouble just one place above Warhammer gold the drop zone in 16th place.

Prior to the goal, United wow po had missed a host of chances as Middlesbrough's wow or defense held firm, but the deadlock was wow po finally broken when Michael Carrick's wow or left-wing cross bounced back off Boro defender wow geld David Wheater into Berbatov's path.

Berbatov, Wayne Rooney, wow geld Cristiano Ronaldo, Rafael, wow gold Nemanja Vidic and Ji-sung Park had earlier all squandered chances buy wow gold against a determined Middlesbrough side.

Rooney should have given the wow gold hosts the lead when he sprang world of warcraft gold the offside trap in the 35th minute but inexplicably chose to pass rather wow gold than shoot.

Then, two minutes wow soldi before half-time, defender Chris Riggott wow gold produced a superbly-timed block inside the penalty area to deny Berbatov buy wow gold just as he was about to shoot.

After Berbatov's goal,wow gold Park produced a contender world of warcraft gold for miss of the season when he wow gold somehow ballooned over the wow oro crossbar from just three yards out from substitute wow gold Gary Neville's cross.

But, despite not adding buy wow gold to their tally, the three points were United's wow gold and their next league game is now against second-placed Chelsea, who are just four points ahead wow power leveling of Sir Alex Ferguson's side

Anonymous said...

che cazzo c'entra l'aston villa.... lo sanno tutti che la persistenza hibernate รจ solo fiorentina!!!

oh fiorentina.... ti tutti i nostri cuori sei la regina.....

pbrende said...

It's pretty sad when World of Warcraft gold farmers are spamming things like this.

Nirmal said...

Check this for a complete understanding of inverse="true"

Gold Guide for World of Warcraft said...

good post :)

- said...

Thanks much!

Anonymous said...

Great explanation who evr creat this, keep it up bro,
Sad for the bunch of idiots posting at end

Anonymous said...

[url=] buy mexitil online in usa[/url]

Anonymous said...

[url=]Viagra online next day shipping[/url]

Anonymous said...

Hi !.
might , probably curious to know how one can manage to receive high yields .
There is no initial capital needed You may start earning with as small sum of money as 20-100 dollars.

AimTrust is what you haven`t ever dreamt of such a chance to become rich
The firm represents an offshore structure with advanced asset management technologies in production and delivery of pipes for oil and gas.

Its head office is in Panama with structures around the world.
Do you want to become a happy investor?
That`s your chance That`s what you really need!

I`m happy and lucky, I began to take up income with the help of this company,
and I invite you to do the same. It`s all about how to choose a proper companion who uses your money in a right way - that`s AimTrust!.
I take now up to 2G every day, and my first deposit was 1 grand only!
It`s easy to join , just click this link
and lucky you`re! Let`s take our chance together to feel the smell of real money

Anonymous said...

miley cyrus nude miley cyrus nude miley cyrus nude

Anonymous said...

Hello!!! is one of the most outstanding innovative websites of its kind. I take advantage of reading it every day. All the best.

Anonymous said...

The author of has written an excellent article. You have made your point and there is not much to argue about. It is like the following universal truth that you can not argue with: You cannot teach someone unless you are willing to learn from them. Thanks for the info.

Anonymous said...

Genial post and this enter helped me alot in my college assignement. Thanks you for your information.

Anonymous said...

Wow, thanks a lot!!! Finally I understood that!!!

Pooja said...

Great post!
Simple explanation for a seemingly difficult topic!

Anonymous said...

good explanation .. thank you

Peter said...

Excelent explanation, simple as water.

Ish Arora said...

excellent post

Anonymous said...

Good post. thanks!

Sambasiva Rao Penugonda said...

Thank u so much...

Anonymous said...

Thanks ..appreciated .. very much