You get a LazyLoadException because NHIbernate - while saving your
functionality instance - invokes the functionality.Group setter to
replace your Group instance with an NHibernate entity proxy. At that
point strange things are going to happen in your code as it currently
stands, such as duplicate entities in your collection... not to
mention that you modify a persistent collection that NHibernate may be
iterating during the saving process, possibly causing enumeration
exceptions as well.

To avoid these strange things you can try to:
- not do anything smart in property setters while your entity is
saving and loading => requires usage of NHibernate lifecycle
functionality or writing NHIbernate interceptor/event handlers to do
something similar.
- use a custom collection that allows you to extend/modify/override
Add or Remove behaviour and make the collection end responsible for
updating the Group property.

I am afraid both approaches require extra work. The custom collection
route may the easiest option as some work is being/has been done in
the area of observable collections types for NHibernate. Maybe someone
else can provide a pointer to suitable options.

Regards,
Gerke.



On Dec 18, 1:37 pm, Vladan Strigo <[email protected]> wrote:
> Hi,
>
> I have set up a standard one to many relation ship between two
> entities:
>
> Functionality <--> FunctionalityGroup
>
> It is mapped in a way that Functionality has a Many-to-One side with
> FunctionalityGroup, while FunctionalityGroup has a One-to-Many with
> Functionality.
>
> My requirements are that when creating a new functionality I assign it
> its functionalitygroup and when showing the report of functionalities
> within a group that I can get the group, load its children
> (functionalities) and show it.
>
> This model supports it just fine, however when creating a new
> functionality I am having a small issue...
>
> In normal situations and all the text book samples it is done like the
> following:
>
> var functionalityGroup = groupRepo.Load(1);
> var functionality = new Functionality;
>
> functionality.Group = functionalityGroup;
> functionalityGroup.Funcitonalities.Add(functionality);
>
> funcRepo.Save(functionality);
>
> This works perfectly so my next step is to move this assignment logic
> on its own:
>
> var functionalityGroup = groupRepo.Load(1);
> var functionality = new Functionality;
>
> functionality.Group = functionalityGroup;
>
> funcRepo.Save(functionality);
>
> where Group property setter (it might as well be SetGroup method or
> something) is:
>
> _group = value;
> _group.Funcitonalities.Add(this);
>
> This brakes with an error like NHibernate.LazyInitializationException
> - Illegal access to collection.
>
> If I turn this around, and move this logic to the Parent in this
> situation - the FunctionalityGroup, it gets a method AddFunctionality:
>
> functionality.Group = this;
> this.Funcitonalities.Add(functionality);
>
> From the other side, this works perfectly...probably because we are
> doing it on the Parent itself.
>
> Now, my question is...is there any way to get it to work in my case
> from above where I get Lazy exception?
> I consider this not to be a standard practice, but more an exception
> in the usage, but still for this case a needed one...
>
> Thanks,
> Vladan
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to