EDIT: I should have mentioned that I'm using NHibernate 1.2 and I've double-checked all the foreign keys in the database tables and they are definitely the correct values.
Hi, I'm new to NHibernate and I've got to grips with the basics but I'm having trouble with collections and would be extremely grateful if anyone could help out. :-)
The general idea is that an Account can have multiple roles (Admin, User, Support etc.) and this is done via an intermediate table (AccountRoles).
The application runs ok, there are no NHibernate configuration exceptions thrown at run-time etc. and the Account details are being pulled from the database successfully, but when I examine the Account.Roles property all I get is an empty {NHibernate.Collection.Generic.PersistentGenericBag<string>}, I think this is where my problem is but I'm not sure how to resolve it, can anyone help please?
I have the following database schema in SQL Server 2005:
Code:
Accounts
Id Identity
EmailAddress VARCHAR(50)
Password VARCHAR(50)
Roles
Id Identity
Name VARCHAR(50)
Description VARCHAR(255)
AccountRoles
Id Identity
AccountId bigint
RoleId bigint
I also have two classes containing properties:
Code:
public class Account : DomainEntity // DomainEntity is an abstract class that has an id field.
{
public Account()
{
Roles = new List<Role>();
}
public virtual IList Roles
{
get; set;
}
...
}
public class Role : DomainEntity // DomainEntity is an abstract class that has an id field.
{
public virtual string Name { get; set; }
public virtual string Description { get; set; }
}
In my Account.hbm.xml file I have this:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SupportConsole.Accounts" assembly="SupportConsole.Accounts">
<class name="Account" table="Accounts">
<id name="Id">
<column name="Id" not-null="true"/>
<generator class="identity"/>
</id>
<property name="AuthenticationAttempts"/>
<property name="AuthenticationCount"/>
<property name="CreatedOn"/>
<property name="EmailAddress"/>
<property name="LastActivityOn"/>
<property name="Name"/>
<property name="Password"/>
<property name="Status"/>
<property name="TelephoneNumber"/>
<bag name="Roles" table="AccountRoles" cascade="none" lazy="false">
<key column="Id" />
<many-to-many class="Role" column="RoleId" />
</bag>
</class>
</hibernate-mapping>
And in my Role.hbm.xml file I have this:
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SupportConsole.Accounts" assembly="SupportConsole.Accounts">
<class name="Role" table="Roles">
<id name="Id">
<column name="Id" not-null="true"/>
<generator class="identity"/>
</id>
<property name="Name"/>
<property name="Description"/>
</class>
</hibernate-mapping>
Thanks!