What's the point of inverse=false?

5 views
Skip to first unread message

James Allen

unread,
Jun 30, 2009, 8:49:18 AM6/30/09
to nhusers
I have been trying to get to grips with Hibernate's inverse attribute,
and it seems to be just one of those things that is conceptually
difficult.

The gist that I get is that when you have a parent entity (e.g.
Parent) that has a collection of Child objects using a one-to-many
mapping, setting inverse=true on the mapping tells Hibernate that 'the
other side (the Child) has responsibility to update itself to maintain
the foreign key reference in its table'.

Doing this appears to have 2 benefits when it comes to adding Children
to the collection in your code, and then saving the Parent: you save
an unneccessary hit on the database (because without inverse set,
Hibernate thinks it has two places to update the FK relationship) (see
http://nhprof.com/Learn/Alert?name=SuperfluousManyToOneUpdate), and
according to the official docs:

"If the column of a association is declared NOT NULL, NHibernate may
cause constraint violations when it creates or updates the
association. To prevent this problem, you must use a bidirectional
association with the many valued end (the set or bag) marked as
inverse="true"."

This all seems to make sense so far. What I don't get is this: when
would you NOT want to use inverse=true on a one-to-many relationship?

Roger

unread,
Jul 1, 2009, 4:30:14 AM7/1/09
to nhusers
<<What I don't get is this: when would you NOT want to use
inverse=true on a one-to-many relationship? >>

In monodirectional relationships.

I would guess the reason this is not "happening by default" in
bidirectional one-to-many refs is to have consistent behaviour with
other mappings such as many-to-many refs where you need to choose one
side. I'm not sure though...

To answer your question - if you have have a bidirectional one-to-many
rel, always use inverse=true.

/Roger

On Jun 30, 2:49 pm, James Allen <james.al...@perfect-image.co.uk>
wrote:
> I have been trying to get to grips with Hibernate's inverse attribute,
> and it seems to be just one of those things that is conceptually
> difficult.
>
> The gist that I get is that when you have a parent entity (e.g.
> Parent) that has a collection of Child objects using a one-to-many
> mapping, setting inverse=true on the mapping tells Hibernate that 'the
> other side (the Child) has responsibility to update itself to maintain
> the foreign key reference in its table'.
>
> Doing this appears to have 2 benefits when it comes to adding Children
> to the collection in your code, and then saving the Parent: you save
> an unneccessary hit on the database (because without inverse set,
> Hibernate thinks it has two places to update the FK relationship) (seehttp://nhprof.com/Learn/Alert?name=SuperfluousManyToOneUpdate), and

Cesar Sanz

unread,
Jul 3, 2009, 4:40:36 PM7/3/09
to nhu...@googlegroups.com
Hello,

What do you mean by biderectional? This topic is very interesting because
this attribute
is really hard to understand.

Can you explain a little further?

Thanks

James Gregory

unread,
Jul 3, 2009, 4:48:59 PM7/3/09
to nhu...@googlegroups.com
Bi-directional relationships are when you can access a relationship from both sides.

// mono-directional
public class Store
{
  public IList<Product> Products { get; }
}

public class Product
{}

// mono-directional
public class Store
{}

public class Product
{
  public Store Store { get; }
}

// bi-directional
public class Store
{
  public IList<Product> Products { get; }
}

public class Product
{
  public Store Store { get; }

Cesar Sanz

unread,
Jul 6, 2009, 3:09:29 PM7/6/09
to nhu...@googlegroups.com
Thanks, this make it clear!!
 
Regards
Reply all
Reply to author
Forward
0 new messages