-->
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.  [ 3 posts ] 
Author Message
 Post subject: failed to lazily initialize a collection, no session or ...
PostPosted: Wed Feb 20, 2008 8:27 pm 
Newbie

Joined: Wed Feb 20, 2008 8:08 pm
Posts: 10
Hi All,

I am newbie to NHibernate so please excuse my ignorance.

I am trying to do a many-to-many mapping for security of a site between a Groups and AccessRights and get the following exception.

{"failed to lazily initialize a collection, no session or session was closed"}

A User has a Group and the Group can have many AccessRights associated with it.

I think it is a problem with my mapping files although I cannot find it. Please find the mapping files below.

The Group will be mapped to the user but unfortunatly the AccessRights (roles) do not seem to be getting populated.

I profiled the application to see if I could see what is going on and there seemed to be no of the AccessRights in the SQL Query (see below).

Any help with this would be really appreciated as I have been chasing my tail with it for the past couple of nights.

Thanks in advance,

Brendan


Code:
exec sp_executesql N'SELECT this_.Id as Id29_2_, this_.Name as Name29_2_, this_.Password as Password29_2_, this_.Email as Email29_2_, group2_.Id as Id30_0_, group2_.Name as Name30_0_, applicatio3_.Id as Id34_1_, applicatio3_.Name as Name34_1_ FROM [User] this_ left outer join [Group] group2_ on this_.Id=group2_.Id left outer join ApplicationScope applicatio3_ on this_.Id=applicatio3_.Id WHERE this_.Name = @p0',N'@p0 nvarchar(4)',@p0=N'Hugh'



Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="HughGRice.Portal.Core.Models.Group, HughGRice.Portal.Core" table="`Group`" lazy="false"  >
      <id name="Id" type="Int32" unsaved-value="null">
         <column name="Id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Group"/>
         <generator class="native" />
      </id>
      <property name="Name" type="String">
         <column name="Name" length="50" sql-type="nvarchar" not-null="false"/>
      </property>

    <bag name="AccessRights" table="GroupAccessRights" cascade="all" >
      <key column="GroupId"/>
      <many-to-many column="AccessRightId" class="HughGRice.Portal.Core.Models.AccessRight, HughGRice.Portal.Core"/>
    </bag>
   </class>
</hibernate-mapping>



Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
   <class name="HughGRice.Portal.Core.Models.AccessRight, HughGRice.Portal.Core" table="AccessRight" lazy="false">
      <id name="Id" type="Int32" unsaved-value="null">
         <column name="Id" length="4" sql-type="int" not-null="true" unique="true" index="PK_AccessRight"/>
         <generator class="native" />
      </id>
      <property name="Name" type="String">
         <column name="Name" length="50" sql-type="nvarchar" not-null="false"/>
      </property>

    <bag name="Groups" table="GroupAccessRights" cascade="all">
      <key column="AccessRightId"/>
      <many-to-many column="GroupId" class="HughGRice.Portal.Core.Models.Group, HughGRice.Portal.Core"/>
    </bag>
   
   </class>
</hibernate-mapping>



Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="true">
  <class name="HughGRice.Portal.Core.Models.User, HughGRice.Portal.Core" table="`User`">
    <id name="Id" type="Int32" unsaved-value="0">
      <column name="Id" length="4" sql-type="int" not-null="true" unique="true" index="PK_User"/>
      <generator class="native" />
    </id>
    <property name="Name" type="String">
      <column name="Name" length="150" sql-type="nvarchar" not-null="false"/>
    </property>
    <property name="Password" type="String">
      <column name="Password" length="150" sql-type="nvarchar" not-null="false"/>
    </property>
    <property name="Email" type="String">
      <column name="Email" length="150" sql-type="nvarchar" not-null="false"/>
    </property>
    <one-to-one name="Group" class="HughGRice.Portal.Core.Models.Group, HughGRice.Portal.Core"/>
    <one-to-one name="ApplicationScope" class="HughGRice.Portal.Core.Models.ApplicationScope, HughGRice.Portal.Core"/>
  </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 10:37 pm 
Beginner
Beginner

Joined: Mon Feb 04, 2008 1:48 pm
Posts: 30
Is this web app? Is this a tiered / layered app?
Can you post you code involving ISession?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 20, 2008 10:57 pm 
Beginner
Beginner

Joined: Mon Feb 04, 2008 1:48 pm
Posts: 30
okay - while I wait for your post here is what I think. A business entity can be transient, persistent, or detached (look this up). when a session is closed the obj becomes detached and needs to be re-attached to a session before using. Check put the lock method on the ISession.

PS. Please rate or give me credit if this helps. Thx


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