I'm using NHibernate 3.2 and I have a problem with mapping a collection.

After querying a user account...

UserAccount userAccount = (from u in Session.Query<UserAccount>()
                           where u.Username == username
                           select u).SingleOrDefault();

... the Role property holds an empty collection while the role field 
contains actual items:

Here are the entity classes:

public class UserAccount : Entity
{
    ...
    private IList<Role> roles;

    public virtual IEnumerable<Role> Roles
    {
        get
        {
            return roles;
        }
    }
    ...
}

public class Role : Entity
{
    ...
}

Here's a part of the HBM mapping file for the UserAccount mapping:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
default-access="property" 
                   auto-import="false" default-cascade="none" 
default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" dynamic-insert="true"
         dynamic-update="true" schema="[MySchema]" mutable="true"
         name="MyNamespace.UserAccount, MyAssembly, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null"
         table="UserAccounts">
    ...
    <bag access="nosetter.camelcase" cascade="none" name="Roles"
         schema="[MySchema]" table="UserAccounts_Roles" mutable="true">
      <key>
        <column name="UserAccountId" />
      </key>
      <many-to-many class="MyNamespace.Role, MyAssembly, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null">
        <column name="RoleId" />
      </many-to-many>
    </bag>
    ...
  </class>
</hibernate-mapping>

And here's part of the HBM that specifies the Role mapping:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
default-access="property"
                   auto-import="false" default-cascade="none" 
default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" dynamic-insert="true"
         dynamic-update="true" schema="[MySchema]" mutable="true"
         name="MyNamespace.Role, MyAssembly, Version=1.0.0.0, 
Culture=neutral, PublicKeyToken=null"
         table="Roles">
    ...
  </class>
</hibernate-mapping>

Here's a link to a question of mine on 
StackOverflow<http://stackoverflow.com/q/6661922/198990>, 
where more details and progress can be seen.

Currently, my findings are:

While debugging the interceptor (which is partially shown below), I noticed 
that when it is called for methodName == "get_Roles", for the TargetInstance 
property (which is a UserAccount in this case), it's roles field is an empty 
collection. Right before accessing the Roles property on the proxy instance 
though, the proxy's role field does have a populated collection.*
*
public class DefaultDynamicLazyFieldInterceptor
    : IFieldInterceptorAccessor, Proxy.DynamicProxy.IInterceptor
{
    ...
    public object Intercept(InvocationInfo info)
    {
        var methodName = info.TargetMethod.Name;

        if (FieldInterceptor != null)
        {
            ...

When checking the InvocationInfo instance, the proxy and the Target 
instances do have a roles field, containing a populated collection.

Thanks,
Sandor.

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/nhusers/-/8zFdQs16YesJ.
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.

Reply via email to