How to avoid the exception "failed to lazily initialize a collection of role NHibernate..." for some properties of the POCO

986 views
Skip to first unread message

Stéphane De Jonghe

unread,
Nov 3, 2009, 5:25:18 AM11/3/09
to nhusers
Hi everybody,
I'm using the version 2.1.0 of NHibernate coupled with Spring.NET:
I have an object called Lang on which I apply a lazy loading
strategie. How to avoid getting the "failed to lazily initialize a
collection of role NHibernate..." on some properties that I don't want
to be loaded everytime?
Here are the hbm and some part of the code: This concerns the
"_ParentPersons" property that by default should return an empty
collection if I don't want to load it explicitly in my DAO.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class
name="Credit.Management.Core.Domain.Lang,Credit.Management.Core"
table="LANG" lazy="true" >

<id name="_Id" column="ID_LANG" type="System.Int32" unsaved-
value="0">
<generator class="native"/>
</id>

<property name="_Code" type="System.String">
<column name="[CODE]" sql-type="nvarchar" not-null="true"
length="15" />
</property>
<property name="_Label" type="System.String">
<column name="[LABEL]" sql-type="nvarchar" not-null="true"
length="50" />
</property>
<property name="_Description" type="System.String">
<column name="[DESCRIPTION]" sql-type="nvarchar" not-null="true"
length="250" />
</property>

<!-- Activ -->
<property name="_Activ" type="System.Boolean">
<column name="[ACTIV]" sql-type="bit" not-null="true" />
</property>
<!-- Activ -->

<property name="_CultureId" type="System.Int32">
<column name="[CULTUREID]" sql-type="int" not-null="true" />
</property>

<bag name="_ParentPersons" table="PERSON" inverse="true"
lazy="true" fetch="select" cascade="none" >
<key column="L_LANG" />
<one-to-many class="Credit.Management.Core.Domain.Person,
Credit.Management.Core"/>
</bag>

</class>
</hibernate-mapping>

Does someone has an idea how to implement this behaviour?

Fabio Maulo

unread,
Nov 4, 2009, 7:40:26 AM11/4/09
to nhu...@googlegroups.com
2009/11/3 Stéphane De Jonghe <sob...@gmail.com>

 How to avoid getting the "failed to lazily initialize a
collection of role NHibernate..." on some properties that I don't want
to be loaded everytime?

Managing the nh-session depending on your needs...
web or WPF ?
--
Fabio Maulo

Graham Bunce

unread,
Nov 4, 2009, 11:36:53 AM11/4/09
to nhusers
> "_ParentPersons" property that by default should return an empty collection if I don't want to load it explicitly in my DAO

My question would be, "why?". As in, why return an empty collection if
you don't want to load it explictly? The data either exists, or it
doesn't so, because Lazy is turned on, NH will get the collection
content (the data) when you access the property. Once its got the data
(or not, if there isn't any) you then have a collection object - which
is either populated or empty.

If you want a collection object to exist by default so that you can
add to it in code, just create the object in the constructor. NH will
overwrite this when it does lazy loading.


Do you want behaviour similar to Entity Framework, whereby you
explictly load the collection when you choose to do so, as opposed to
NH deciding when to load it? if so, I think this would be an explict
HQL/Criteria/Linq call to load the data for the Person object. I'm not
sure there is an equvilant mapping option in the HBM.



Stéphane De Jonghe

unread,
Nov 5, 2009, 5:04:13 AM11/5/09
to nhusers
Well I finally understood the principle with the lazy loady problem
that I encounter both in a WCF service and Winform application and
because theses applications were scanning all properties from my POCO
by reflection, properties of type List, if they were not explicitly
loaded, returned an lazy loading exception (let's say that you don't
need them in a master list but only in a detail form).
But with the help of the projection, I'm now able to fine tune
properties that will be loaded by default and to have a default value
(like an empty collection) for the others. Then I always can load them
later by using the NHibermateUtils.Initialize...

Thanks anyway for your reactions.

Fabio Maulo

unread,
Nov 5, 2009, 7:12:29 AM11/5/09
to nhu...@googlegroups.com
If you use NHibernateUtils.Initialize you still have the same problem...
Your problem is a good session management.

2009/11/5 Stéphane De Jonghe <sob...@gmail.com>



--
Fabio Maulo

Stéphane De Jonghe

unread,
Nov 5, 2009, 7:22:10 AM11/5/09
to nhusers
Yes ,exactly and because we are working with several layers (Core,
Data, Service and Presentation), each time I will have to get more
properties of an POCO, this will be done through the same principle of
accessing the service which uses the DAO and the session management is
handled in our case by Spring.
Thanks.
Reply all
Reply to author
Forward
0 new messages