如何在nhibernate中映射有序列表?

Ela*_*nda 5 c# database nhibernate

我有两个类:container其中包含动态有序列表Element.

我应该使用什么C#集合?

您建议我使用什么数据库架构?

我该如何配置nhibernate映射?

TIA

Ger*_*ard 5

NHibernate支持System.Collections.SortedList和Iesi.Collections.SortedSet实现的集合.您必须在映射文件中指定比较器:

<set name="Aliases" table="person_aliases" sort="natural">
    <key column="person"/>
    <element column="name" type="String"/>
</set>

<map name="Holidays" sort="My.Custom.HolidayComparer, MyAssembly" lazy="true">
    <key column="year_id"/>
    <index column="hol_name" type="String"/>
    <element column="hol_date" type="Date"/>
</map>
Run Code Online (Sandbox Code Playgroud)

sort属性的允许值是unsorted,natural和实现System.Collections.IComparer的类的名称.

如果您希望数据库本身对集合元素进行排序,请使用set,bag或map映射的order-by属性.这将在SQL查询中执行排序,而不是在内存中执行.

设置order-by属性告诉NHibernate在内部使用ListDictionary或ListSet类来表示字典和集合,从而维护元素的顺序.请注意,如果这些集合包含多个元素,则对这些集合的查找操作非常慢.

<set name="Aliases" table="person_aliases" order-by="name asc">
    <key column="person"/>
    <element column="name" type="String"/>
</set>

<map name="Holidays" order-by="hol_date, hol_name" lazy="true">
    <key column="year_id"/>
    <index column="hol_name" type="String"/>
    <element column="hol_date type="Date"/>
</map>
Run Code Online (Sandbox Code Playgroud)

请注意,order-by属性的值是SQL排序,而不是HQL排序!

甚至可以在运行时使用Filter()按照某些任意标准对关联进行排序.

sortedUsers = s.Filter( group.Users, "order by this.Name" );
Run Code Online (Sandbox Code Playgroud)

来源: NHibernate和Sorted Collections