Your problem is not lazy loading but the session-management (well... only to give mention to one) and the bug is there for sure but in your application and not in NHibernate.
2010/3/15 SedulousTurtle <[email protected]> > And I should also note: of course, using lazy="false" in the > Institution class mapping > (as in: <class name="Institution" lazy="false">) > keeps the exception from being thrown...but I can't make this the > default fix whenever I get a bug like this! > > > On Mar 15, 4:16 pm, SedulousTurtle <[email protected]> > wrote: > > Hi List, > > > > I'm responsible for looking at NHibernate as a potential DB solution > > on our next product release -- but I'm having trouble getting my > > prototype to work properly. In a few cases, I'm getting perplexing > > behaviour associated with lazy loading and proxies. I suspect that > > these are all occuring because I have some error in my understanding > > of how these work. > > > > Here's my first problem. I'm encountering a > > LazyInitializationException when my WebApp testing front end tries to > > display a DataGrid with information about objects of type Instrument. > > > > ---------------- Application information ------------------ > > The application manages electronic medical instruments in a hospital. > > > > Part of our object system is a heiarchy of parent/child relationships > > (parent owns a collection of children; children have a reference to > > their parent; I don't think either end is set inverse="true") > > > > Institution (e.g. Springfield Hospital) > > Location (e.g. Emergency Room) > > Instrument > > > > Institution and Location are effectively "virtual" classes (though > > they're not explicitly marked as such); they each have 2 subclasses > > that are used in the database. > > > > References in the source code to other objects such as > > HealthCareSystem and Method are unimportant. > > > > ------------------------------------ Source Code > > ------------------------------------------- > > <Locations.aspx.cs> > > ... > > private void BindInstrumentGrid() > > { > > ... > > > > grInstr.DataSource = > > Facade.InstLoc.GetInstruments(<various parameters>); > > grInstr.DataBind(); > > } > > > > <InstrumentFacade.cs> > > ... > > public IList GetInstruments(<various parameters>) > > { > > IList<Instrument> instrumentList; > > ... > > > > using (ISession ses = NHibernateHelper.OpenSession()) > > using (ses.BeginTransaction()) > > { > > instrumentList = ses.CreateQuery( > > @"SELECT instr FROM Instrument AS instr > > JOIN instr.Method AS md > > JOIN md.Method AS meth > > JOIN instr.Location AS loc > > JOIN loc.Institution AS instit > > JOIN instit.HCSystem AS hcs > > WHERE meth.Name=:method_name > > AND loc.Name=:location_name > > AND instit.Name=:institution_name > > AND hcs.Name=:healthcaresystem_name") > > .SetString("method_name", methodName) > > .SetString("location_name", asLocationName) > > .SetString("institution_name", asInstName) > > .SetString("healthcaresystem_name", > > asHCSystemName) > > .List<Instrument>(); > > > > foreach (Instrument i in instrumentList) > > { > > NHibernateUtil.Initialize(i.Location); > > NHibernateUtil.Initialize(i.Location.Institution); > > } > > > > ses.Transaction.Commit(); > > } > > return instrumentList.ToList(); > > } > > ... > > > ---------------------------------------------------------------------------------------------------- > > > > On application execution, when BindInstrumentGrid() executes > > "grInstr.DataBind();" I get the following NHibernate exception: > > "Initializing[TestRecordProto.Domain.Institution#2]-Could not > > initialize proxy - no Session." > > > > I partially understand why I'm getting this error, because I'm seeing > > weird behavior during NHibernateUtil.Initialize() when I run in debug > > mode. Initially, i.Location is of type LocationProxy -- with its only > > set property being "string Name". All other properties, including the > > int ID, is uninitialized. But after "Initialize(i.Location)", I would > > expect i.Location proxy would be replaced with a real object of type > > Location. But it is not in fact replaced -- the reference is left as > > a proxy, and its properties do not change! And of course then > > "Initialize(i.Location.Institution)" can not really initialize the > > Institution field -- because i.Location.Institution is null, because > > i.Location is a proxy without instantiated properties! > > > > So I don't know what I'm doing wrong -- I'm supposed to call > > Initialize() on the referenced objects that I'll need to access after > > the session is closed -- and yet Initialize() isn't solving the > > problem for me. Why does this happen? > > > > The information in the database all looks correct, with the correct > > links between these objects. I have 1 record saved of type > > Instrument, which is linked by foreign key to a valid Location, which > > is linked by foreign key to a valid Institution. > > > > Thank you in advance. > > -- > 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]<nhusers%[email protected]> > . > For more options, visit this group at > http://groups.google.com/group/nhusers?hl=en. > > -- Fabio Maulo -- 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.
