@Oskar,
Thanks for the explanation..
>From another thread I extracted this
"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'"
And I think it has to do with what you said before
" If you now do myParent.Children.Add(newChild) this will not be
persisted because Children is mapped with inverse=true. In order to
get the new association persisted you have to do newChild.Parent =
myParent"
Well, having this two paragraphs, I can conclude this (please, correct me if
I am wrong)
1. With inverse=true, the many end (Children in this case) has to set,
explicitly, the parent to which it belongs.. (e.g Child.Parent = parent)
2. With inverse=true, the many end (Children in this case) has no obligation
to set the parent to which it belongs..
BTW, having a biderectional relationship, I saw somebody writing this code,
which I think is cool.
public class Parent
{
...
private IList<Child> _children;
public IList<Child> Children {get {return _children; }; } // NOTE ONLY
"GET" IN THIS PROPERTY, NO SETTER
public void AddChild(Child child)
{
child.Parent = this;
_children.Add(child);
}
}
----- Original Message -----
From: "Oskar Berggren" <[email protected]>
To: <[email protected]>
Sent: Friday, August 21, 2009 1:01 AM
Subject: [nhusers] Re: Inverse=True (again)
>
> Some answers below, hopefully correct. :)
>
>
> 2009/8/20 spiralni <[email protected]>:
>>
>> I don't get it :(
>>
>> I've read, and test all the references in the web, but this concept is
>> just to complex for me.. Hope you can clarify me.
>>
>> From the 2.1 Reference, I found this in the Bidirectional Associations
>> topics,
>>
>> 1. "Please note that NHibernate does not support bidirectional one-to-
>> many associations with an indexed collection (list, map or array) as
>> the "many" end, you have to use a set or bag mapping. "
>>
>
> I believe this is because if the many-side (the collection) is mapped
> with inverse, it isn't persisted, therefore its knowledge of the index
> would be lost anyway. The other side just has a direct object
> reference, so no info about the index there either.
>
> Is it possible to have a bidirectional one-to-many where the many side
> is not inverse?
>
>
>> 2. "Changes made only to the inverse end of the association are not
>> persisted."
>>
>> Think about client and products, if products is marked with Inverse =
>> true, cannot I change the product itselft, must it to be updated via
>> client..
>
>
> ICollection Parent.Children is inverse
> Parent Child.Parent is not inverse.
>
> If you now do myParent.Children.Add(newChild) this will not be
> persisted because Children is mapped with inverse=true. In order to
> get the new association persisted you have to do newChild.Parent =
> myParent. This will be saved by NHibernate. (It would be good form to
> _also_ add it to the Parent's Children collection, otherwise it won't
> be visible there until you have reloaded the Parent object fresh from
> the database.
>
> Other properties of Parent (in your case Product) is of course not
> affected by this and will be updated as necessary when dirty.
>
>
>>
>> 4. "Mapping one end of an association with inverse="true" doesn't
>> affect the operation of cascades, both are different concepts! "
>>
>> whaaaat????
>>
>
>
> With Parent/Child as above, suppose that Parent.Children is also
> mapped with cascade ="all-delete-orphan".
>
> If you now do session.Delete(myParent), the delete will cascade to all
> children, so that they are also deleted. This behaviour is the same
> regardless of the inverse setting.
>
> Likewise, if you create a new Parent and a new child and do:
> newChild.Parent = newParent;
> newParent.Children.Add(newChild); // not necessary for NHibernate in
> this case but good form.
> session.Add(newParent);
>
> Then the new child will also be persisted, even though we haven't done
> session.Add(newChild) ourselves.
>
>
> /Oskar
>
> >
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---