NHibernate:如何映射有序的多对多关系

时间:2009-12-11 06:15:56

标签: nhibernate

我正在尝试找出最好的方法(即最简单和万无一失)来映射多对多的集合,这些集合在检索时应该进行排序。

这是基本实体和表格:

public class Person
{
    private IList<AddressHistoryRecord> _addressHistory = new List<AddressHistoryRecord>();

    IList<AddressHistoryRecord> AddressHistory
    {
        get { return _addressHistory; }
        set { return _addressHistory; }
    }
}

public class AddressHistoryRecord
{
    public Guid Id { get; set; }
    public Guid AtAddressSince { get; set; }
    ...
}

表格是:

table t_Person { ... }
table t_PersonAddressHistory { PersonId, AddressHistoryRecordId }
table t_AddressHistoryRecord { Id, AtAddressSince ... }

所以我希望能够根据子 AddressHistoryRecord 表的 AtAddressSince 列以排序顺序检索Person的地址历史记录。什么是我最好的选择?

1 个答案:

答案 0 :(得分:2)

我倾向于使用“idbag”作为我的多对多映射,但下面的示例将适用于普通包:

<idbag name="AddressHistory" table="t_PersonAddressHistory" lazy="true" cascade="all">
    <collection-id column="PersonAddressHistoryId" type="Int64"> <!--PersonAddressHistoryId is the t_PersonAddressHistory primary key. idbag requires the X table to have its own primary key. --> 
        <generator class="hilo" />
    </collection-id>
    <key column="PersonId" />
    <many-to-many class="AddressHistoryRecord" column="AddressHistoryRecordId" order-by="AtAddressSince"/>
</idbag>

订购发生在多对多标签中......

请参阅NHibernate的文档,了解有关idbag的更多信息,以及它对普通包有什么好处,但这与您的问题无关。