These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: LazyInitializationException
PostPosted: Thu Nov 02, 2006 5:19 am 
Newbie

Joined: Thu Nov 02, 2006 4:59 am
Posts: 1
Hi there

in my quest after stuff that makes my life as a programmer easier and keeps me away from databases (for my colleagues' sakes) I am looking at nHibernate. Right now I get an exception when I try to access an object instantiated by hibernate.

I obviously don't understand something about hibernate "session" handling.

My code snippet below results in the exception below. If I un-comment the commented line then I do not see the exception. (When I write the Stat object to debug, then the "ToString" method of the Stat object accesses all the fields of the object - does this have an effect on the exception I see?)

In any case it appears that an object instantiated by hibernate really should only be accessed inside a hibernate session - is that correct? How then can I isolate my data-access code in a "DAL" whereby the calling code has no knowledge of where the object actually comes from, only that it calls "GetStat()" and it gets a Stat object back that it can use - without having to worry about closing a hibernate session for example?


Hibernate version:
1.2.0.2001

Mapping documents:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="Data.Stat, Data" table="statisticslog">
<id name="Id" column="id" type="int">
<generator class="assigned" />
</id>
<property name="Date" column="date" type="DateTime" />
<property name="StatisticType" column="StatisticType" type="int" />
<property name="UserId" column="UserId" type="String" length="50"/>
</class>
</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
Configuration cfg = new Configuration();
cfg.AddAssembly("Data");
ISessionFactory sessionFactory = cfg.BuildSessionFactory();
ISession session = sessionFactory.OpenSession();
ITransaction transaction = session.BeginTransaction();
Stat aStat = (Stat)session.Load(typeof(Stat), 50);
log.Debug("" + aStat.Id);
// log.Debug("aStat=" + aStat);
session.Close();
log.Debug("aStat=" + aStat);

Full stack trace of any exception that occurs:
NHibernate.LazyInitializationException: Could not initialize proxy - the owning Session was closed.
at NHibernate.Proxy.LazyInitializer.Initialize() in c:\net\nhibernate-1.2.0.Beta1\nhibernate\src\NHibernate\Proxy\LazyInitializer.cs:line 81
at NHibernate.Proxy.LazyInitializer.GetImplementation() in c:\net\nhibernate-1.2.0.Beta1\nhibernate\src\NHibernate\Proxy\LazyInitializer.cs:line 240
at NHibernate.Proxy.CastleLazyInitializer.Intercept(IInvocation invocation, Object[] args) in c:\net\nhibernate-1.2.0.Beta1\nhibernate\src\NHibernate\Proxy\CastleLazyInitializer.cs:line 59
at CProxyTypeStatData_INHibernateProxy1.ToString()
at System.String.Concat(Object arg0, Object arg1)
at App.App.Main(String[] args) in g:\documents - work\productivity tools and other utilities and stuff\object relational mapping\nhibernate\program\solution1\app\app.cs:line 47

Name and version of the database you are using:
SQL Server

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Thanks,
Peter


Top
 Profile  
 
 Post subject: Re: LazyInitializationException
PostPosted: Fri Nov 03, 2006 4:28 pm 
Beginner
Beginner

Joined: Wed Aug 03, 2005 8:06 am
Posts: 40
Location: Netherlands
Hi,

As you see in the exception your class is a proxy, which I believe is the default for class mappings since v. 1.2.0. This means that the Id is known, and only if any other property is required, the database is hit to actually load your instance.

So, no, you can use your object without any knowledge of NHibernate, but in your DAL you have to make sure the proxy gets initialized, e.g. by NHibernateUtil.Initialize(), or otherwise mark your class as lazy=false. The same goes for any lazy collections you'll need beyond your DAL.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.