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.  [ 2 posts ] 
Author Message
 Post subject: Basic Collection Mapping
PostPosted: Mon Nov 17, 2008 9:29 am 
Newbie

Joined: Mon Nov 17, 2008 9:03 am
Posts: 4
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!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 17, 2008 12:00 pm 
Newbie

Joined: Mon Nov 17, 2008 9:03 am
Posts: 4
I fixed it and thought I'd post the solution in case anyone else had a similar problem, it turns out that I was using the wrong id:

Code:
<bag name="Roles" table="AccountRoles" cascade="none" lazy="false">
         <!-- The line below should be <key column="AccountId" />-->
         <key column="Id" />
         <many-to-many class="Role" column="RoleId" />
      </bag>


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