[fluent-nhib] What is the purpose of Inverse() ?

4,796 views
Skip to first unread message

Nabeel

unread,
May 12, 2010, 12:33:05 PM5/12/10
to Fluent NHibernate
Hi guys,

I am exploring Fluet NHibernate and I have not used NHibernate before.
I would like to know what is the purpose of Inverse() method.

In the example on wiki. it is stated

"Inverse on HasMany is an NHibernate term, and it means that the other
end of the relationship is responsible for saving."

But even if I remove the Inverse() from the StoreMap in the following
statement

HasMany(x => x.Staff)
.Inverse()
.Cascade.All();

it still works and generate the same sql.

Also, Store has one-to-many on Employee, so how come employee be
responsible for saving itself? It needs a StoreId as its foreign key
rite, so it should be the other way round i.e. Store should be
responsible for saving employee ?

I am lost? The strange thing is even after removing it, the example
works as it is.

--
You received this message because you are subscribed to the Google Groups "Fluent NHibernate" group.
To post to this group, send email to fluent-n...@googlegroups.com.
To unsubscribe from this group, send email to fluent-nhibern...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.

TigerShark

unread,
May 13, 2010, 11:28:03 AM5/13/10
to Fluent NHibernate
Inverse has to do with the way NHibernate treats inserts and updates.
With inverse set to true, you are defining the "other" side of the
relation as the owner.
It is very confusing and hard to understand, unless you do some tests
with inserts.

With a collection, you can tell NHibernate to, as you did, cascade the
updates and inserts.
If you are inserting an entity in a collection and saving the class
containing the collection, NHibernate can do one of two things.

1. insert parent and then insert the child (inverse true), or
2. insert the child, insert the parent, update the foreign key of the
child (inverse false)

It all depends on your mapping and object model, but just remember
that by defining inverse, you define which side of the relationship
that "owns" it.

You can also find a lot of information regarding this on Google :)
Check also http://stackoverflow.com/questions/713637/inverse-attribute-in-nhibernate


Kenneth

TigerShark

unread,
May 13, 2010, 11:34:34 AM5/13/10
to Fluent NHibernate
This is also a very nice explanation
http://www.emadashi.com/index.php/2008/08/nhibernate-inverse-attribute/

On 12 Maj, 18:33, Nabeel <nabeelfa...@gmail.com> wrote:

Nabeel

unread,
May 14, 2010, 7:50:28 PM5/14/10
to Fluent NHibernate
Thanks Kenneth, That link you mentioned explains it pretty well.

Regards,
Nabeel

On May 13, 4:34 pm, TigerShark <kenneth.siew...@gmail.com> wrote:
> This is also a very nice explanationhttp://www.emadashi.com/index.php/2008/08/nhibernate-inverse-attribute/
>
> On 12 Maj, 18:33, Nabeel <nabeelfa...@gmail.com> wrote:
>
>
>
>
>
> > Hi guys,
>
> > I am exploring Fluet NHibernate and I have not used NHibernate before.
> > I would like to know what is the purpose ofInverse() method.
>
> > In the example on wiki. it is stated
>
> > "Inverseon HasMany is an NHibernate term, and it means that the other
> > end of the relationship is responsible for saving."
>
> > But even if I remove theInverse() from the StoreMap in the following

Mohammad Reza Pazooki

unread,
Dec 14, 2014, 1:33:23 AM12/14/14
to fluent-n...@googlegroups.com, kenneth...@gmail.com
Tnx @TigerSgark for your help. I just wanna insure to get your right. your mean when I put inverse() in my mapping it means that this class in parent and first this class must be saved and then their child collections ?
Reply all
Reply to author
Forward
0 new messages