views:

201

answers:

1

I'm trying to create a simple User object that has a Friends property that points to an IList.

Started off just trying to get an ISet to work.

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false"
                   assembly="MyProject.Domain" namespace="MyProject.Domain">
  <class name="User" table="[User]">
    <id name="Id" column="UserId">
      <generator class="increment" />
    </id>
    <property name="UserName" column="UserName" />
    <property name="Location" />
    <set name="Friends" table="Friend" lazy="false">
      <key column="UserId" />
      <many-to-many class="User" column="FriendId" />
    </set>
  </class>
</hibernate-mapping>

This seems to be working ok. But how would I convert this to a IList and maybe sort the Friends by the date the relationship was established (in reverse chronological order) Can I add a "sort by" field to the Friend table?

Here is my class so far -

public class User
{
    public virtual int Id { get; set; }
    public virtual string UserName { get; set; }
    public virtual string Location { get; set; }
    public virtual ISet<User> Friends { get; set; }

}
+2  A: 

instead of the set mapping you can use the Bag mapping to give you an IList. You can also add an order-by element to the bag mapping as specified in the NHibernate documentation

<map
    name="propertyName"                                         (1)
    table="table_name"                                          (2)
    schema="schema_name"                                        (3)
    lazy="true|false"                                           (4)
    inverse="true|false"                                        (5)
    cascade="all|none|save-update|delete|all-delete-orphan"     (6)
    sort="unsorted|natural|comparatorClass"                     (7)
    order-by="column_name asc|desc"                             (8)
    where="arbitrary sql where condition"                       (9)
    fetch="select|join"                                         (10)
    batch-size="N"                                              (11)
    access="field|property|ClassName"                           (12)
>

    <key .... />
    <index .... />
    <element .... />
</map>
lomaxx
+1 thanks. I'll try that out today and update with answer/comments.
tyndall
Lomaxx, I'm I escaping the table name User correctly? or is there a better way to do this in a config file somewhere? It gives me an error when I don't use the [ ] brackets.
tyndall
yes i believe you are
lomaxx