user17510
user17510

Reputation: 1549

NHibernate Mapping intermediary table

Hi I'm looking for some help in mapping the following tables to a hibernate mapping file: schema image http://barry-jones.com/temp/sch2.jpg

I'm trying to fill the localCalandarGroups List with related localCalendarGroups when loading my userVOs. I'm using a intermediary table(user_localCalendarGroup) to keep the user and localCalendarGroup ids

 public class UserVO
{
    public virtual int id { get; set; }
    public virtual string name { get; set; }
    public virtual string pass { get; set; }
    public virtual string email { get; set; }
    public virtual string level { get; set; }
    public virtual List<LocalCalendarGroupVO> localCalendarGroups { get; set; }
}
public class LocalCalendarGroupVO
{

    public virtual int id { get; set; }
    public virtual string name { get; set; }

}

This is my mapping file thus far. How can I make NHibernate aware of the intermediary table?

 <class name="UserVO" table="tb_users" lazy="false">

<id name="id" column="id">

  <generator class="native" />

</id>

<property name="name" />
<property name="pass" />
<property name="level" />
<property name="email" />
<property name="localCalendarGroups"/>

Any help,pointers much appreciated.

Upvotes: 0

Views: 325

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039438

The first thing I would suggest you is using IList instead of List for mapping the localCalendarGroups association in your domain classes. This will enable lazy loading the collection. And here's the mapping file for the UserVO class:

<class name="UserVO" table="tb_users">
    <id name="id" column="id">
        <generator class="native" />
    </id>
    <property name="name" />
    <property name="pass" />
    <property name="level" />
    <property name="email" />

    <bag name="localCalendarGroups" table="user_localCalendarGroup">
        <key column="userID" />
        <many-to-many class="LocalCalendarGroupVO" column="calID" />
    </bag>
</class> 

And the corresponding class definition:

public class UserVO
{
    public virtual int id { get; set; }
    public virtual string name { get; set; }
    public virtual string pass { get; set; }
    public virtual string email { get; set; }
    public virtual string level { get; set; }
    public virtual IList<LocalCalendarGroupVO> localCalendarGroups { get; set; }
}

Upvotes: 4

Related Questions