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>