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.  [ 5 posts ] 
Author Message
 Post subject: Problem with Collection loading
PostPosted: Fri Sep 21, 2007 11:18 am 
Newbie

Joined: Fri Sep 21, 2007 11:09 am
Posts: 5
Hibernate version:1.2

Mapping documents:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false">
<class name="DosRios.Core.PortalManagement.Portal, DosRios.Core" table="Portal">
<id name="Id" type="Guid" unsaved-value="null">
<column name="Id" length="16" sql-type="uniqueidentifier" unique="true" index="PK_Portal"/>
<generator class="guid" />
</id>
<property name="Name" type="String">
<column name="Name" length="255" sql-type="nvarchar"/>
</property>
<property name="CultureInfo" type="String">
<column name="CultureInfo" length="50" sql-type="nvarchar"/>
</property>
<property name="SysCreate" type="DateTime" generated="insert">
<column name="SysCreate" length="8" sql-type="datetime"/>
</property>
<property name="SysCreateUser" type="Guid">
<column name="SysCreateUser" length="16" sql-type="uniqueidentifier"/>
</property>
<property name="SysUpdate" type="DateTime">
<column name="SysUpdate" length="8" sql-type="datetime"/>
</property>
<property name="SysUpdateUser" type="Guid">
<column name="SysUpdateUser" length="16" sql-type="uniqueidentifier"/>
</property>
<bag name="PortalURLs" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="PortalId"/>
<one-to-many class="DosRios.Core.PortalManagement.URL, DosRios.Core"/>
</bag>
<bag name="PortalCultures" inverse="true" lazy="true" cascade="all-delete-orphan">
<key column="PortalId" />
<one-to-many class="DosRios.Core.PortalManagement.PortalCulture, DosRios.Core" />
</bag>
</class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="false">
<class name="DosRios.Core.PortalManagement.PortalCulture, DosRios.Core" table="PortalCulture">
<id name="Id" type="Guid" unsaved-value="null">
<column name="Id" length="16" sql-type="uniqueidentifier" unique="true"/>
<generator class="guid" />
</id>

<property name="Culture" type="String">
<column name="Culture" length="50" sql-type="nvarchar"/>
</property>
<property name="DisplayName" type="String">
<column name="DisplayName" length="500" sql-type="nvarchar"/>
</property>
<property name="SysCreate" type="DateTime" generated="insert">
<column name="SysCreate" sql-type="datetime"/>
</property>
<property name="SysCreateUser" type="String">
<column name="SysCreateUser" length="50" sql-type="nvarchar"/>
</property>
<property name="SysUpdate" type="DateTime">
<column name="SysUpdate" sql-type="datetime"/>
</property>
<property name="SysUpdateUser" type="String">
<column name="SysUpdateUser" length="50" sql-type="nvarchar"/>
</property>

<many-to-one name="Portal" class="DosRios.Core.PortalManagement.Portal, DosRios.Core">
<column name="PortalId" length="16" sql-type="uniqueidentifier" not-null="true" />
</many-to-one>

</class>
</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():

Dim portalService As PortalService(Of Portal) = New PortalService(Of Portal)()
Dim urlService As URLService(Of URL) = New URLService(Of URL)()
Dim url As URL = urlService.LoadUrl(environment)
Dim portal As Portal = url.Portal

Dim portalCultures As IList = portal.PortalCultures
Dim i As Int32 = portalCultures.Count


Full stack trace of any exception that occurs:

An exception of type 'NHibernate.LazyInitializationException' occurred in NHibernate.DLL but was not handled in user code

Additional information: Failed to lazily initialize a collection



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

I get the above exception when I attempt to access the portalCultures.Count property. I have used SQL Profiler to check the SQL that is being generated and when I access the PortalCultures colletion off the portal instance, the SQL that is run returns to records. It's as if there is some type of mapping problem between the resultset from the database to the .NET objects. Does anyone have any suggestions? Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 12:17 pm 
Newbie

Joined: Fri Sep 21, 2007 11:09 am
Posts: 5
I first thought that the Session was closed, however I used the watch window and it shows the Session is still open when I attempt to get the count of the PortalCultures. Any thoughts? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 12:29 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Is your URL service that performs LoadURL() using a separate session? If so, that session will be closed by the time you try to access the count. You either need to use the same session or force initialization within LoadURL() on everything that the caller might need. In most cases the latter is not practical.

We made our own session/transaction scope mechanism similar to System.Transactions.TransactionScope in .NET 2.0, but it was a major hassle to develop. There are frameworks out there for you to pick up and join the "current" session and transaction, but I'm not familiar with them or what their names are. I'm sure someone can point us in the right direction on this ...


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 21, 2007 12:37 pm 
Expert
Expert

Joined: Fri Oct 28, 2005 5:38 pm
Posts: 390
Location: Cedarburg, WI
Oh, I forgot to mention -- proxies remember the session that they were created in. So if urlService.LoadUrl() uses its own session which it promptly closes, then entities (and their collections) loaded but not initialized by it will not be able to further lazy initialize unless you "reattach" them to another open session. This reattachment can be a real pain, because NHibernate provides no mechanism to resolve conflicts when a different instance of the "same" entity (same ID) that you're trying to reattach is already in the session's cache.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 22, 2007 4:30 pm 
Newbie

Joined: Fri Sep 21, 2007 11:09 am
Posts: 5
We are currently using an HttpModule to open the session on BeginRequest and flush/close on the EndRequest, so the session is still available at the point of execution. Any other thoughts?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.