views:

26

answers:

1

Hi All

I am trying to map Users to each other. The senario is that users can have buddies, so it links to itself

I was thinking of this

  public class User
    {
        public virtual Guid Id { get; set; }
        public virtual string FirstName { get; set; }
        public virtual string LastName { get; set; }
        public virtual string EmailAddress { get; set; }
        public virtual string Password { get; set; }
        public virtual DateTime? DateCreated { get; set; }
        **public virtual IList<User> Friends { get; set; }**
        public virtual bool Deleted { get; set; }
    }

But am strugling to do the xml mapping.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="MyVerse.Domain"
                   namespace="MyVerse.Domain" >
  <class name="User" table="[User]">
    <id name="Id">
      <generator class="guid" />
    </id>
    <property name="FirstName" />
    <property name="LastName" />
    <property name="EmailAddress" />
    <property name="Password" />
    <property name="DateCreated" />
    <property name="Deleted" />
    <set name="Friends" table="UserFriend">
      <key foreign-key="Id"></key>
      <many-to-many class="User"></many-to-many>
    </set>
  </class>
</hibernate-mapping>
+1  A: 

something like

 <bag name="Friends" table="assoc_user_table" inverse="true" lazy="true" cascade="all">
  <key column="friend_id" />
  <many-to-many class="User,user_table" column="user_id" />
</bag>
jim
Should it not be many to many?
john
sorry, I editited
jim
Ok so this works but how would I query it?
john
what do you mean? User u = new User(); var friendList = u.Friends;
jim
Initializing[MyVerse.Domain.User#76d7c206-6f32-44e4-a438-4fe17eeb43e1]-failed to lazily initialize a collection of role: MyVerse.Domain.User.Friends, no session or session was closed
john
interesting...this happens when you query it? try turning off lazy loading and let's see what happens.
jim
@john: make sure you access the collection before you close the session. Since it is lazy loaded, NH will need to go back to the database to load the entities. Or you could eagerly load them.
Kent Boogaart