NHibernate Step By Step(7)-映射集合类型
2009-08-06 09:37 Clingingboy 阅读(739) 评论(2) 编辑 收藏 举报1.Set 无序不重复对象 如ISet
2.List 可按位索引对象 如IList
3.Map 键值对象,如HashTable
4.Bag 可重复对象 如IList
5.idbag
示例
1.Set
<class name="A" table="a" lazy="false"> <id name="Id" column="id" unsaved-value="null"> <generator class="native" /> </id> <property name="Name" column="aname" /> <set name="Items" cascade="all-delete-orphan" generic="true"> <key column="a_id" /> <one-to-many class="B" /> </set> </class> <class name="B" table="b" lazy="false"> <id name="Id" column="id" unsaved-value="null"> <generator class="native" /> </id> <property name="Name" column="aname" /> </class>
<key column="a_id" /> 指定了外键值
<one-to-many class="B" /> 指定集合关联
2.List
<class name="A" table="a" lazy="false"> <id name="Id" column="id" unsaved-value="null"> <generator class="native" /> </id> <property name="Name" column="aname" /> <list name="Items" cascade="all-delete-orphan" generic="true"> <key column="a_id" /> <index column="a_idx" /> <one-to-many class="B" /> </list> </class> <class name="B" table="b" lazy="false"> <id name="Id" column="id" unsaved-value="null"> <generator class="native" /> </id> <property name="Name" column="aname" /> </class>
因为有索引,所以加了一个index的索引字段,这是必须的
3.Bag
<class name="A" table="a" lazy="false"> <id name="Id" column="id" unsaved-value="null"> <generator class="native" /> </id> <property name="Name" column="aname" /> <bag name="Items" cascade="all-delete-orphan" generic="true"> <key column="a_id" /> <one-to-many class="B" /> </bag> </class> <class name="B" table="b" lazy="false"> <id name="Id" column="id" unsaved-value="null"> <generator class="native" /> </id> <property name="Name" column="aname" /> </class>
4.Map
<class name="A" table="a"> <id name="Id" column="id" unsaved-value="null"> <generator class="native" /> </id> <property name="Name" column="aname" /> <map name="Items" cascade="all-delete-orphan"> <key column="a_id" /> <index column="idx" type="String" /> <one-to-many class="B" /> </map> <map name="SortedList" collection-type="sorted-list" sort="natural"> <key column="a_id" /> <index column="idx" type="string" /> <element column="elt" type="int" /> </map> <map name="SortedDictionary" collection-type="sorted-dictionary" sort="natural"> <key column="a_id" /> <index column="idx" type="string" /> <element column="elt" type="int" /> </map> </class> <class name="B" table="b" lazy="false"> <id name="Id" column="id" unsaved-value="null"> <generator class="native" /> </id> <property name="Name" column="aname" /> </class>
(1)A与B关系
![image image](https://images.cnblogs.com/cnblogs_com/Clingingboy/WindowsLiveWriter/NHibernateStepByStep7_150E2/image_thumb_1.png)
(2)element表示获取数据库字段集合,而非对象集合,如下映射的类
public class A { private int? _id; private string _name; private IDictionary<string, B> _items; private IDictionary<string, int> _sortedList; private IDictionary<string, int> _sortedDictionary; public A() { } public int? Id { get { return _id; } set { _id = value; } } public string Name { get { return _name; } set { _name = value; } } public IDictionary<string,B> Items { get { return _items; } set { _items = value; } } public IDictionary<string, int> SortedList { get { return _sortedList; } set { _sortedList = value; } } public IDictionary<string, int> SortedDictionary { get { return _sortedDictionary; } set { _sortedDictionary = value; } } }
(3)索引
key和value肯定有索引所以要加所以字段
(4)排序
4.1 sort字段
<map name="SortedList" collection-type="sorted-list" sort="natural">
sort属性中允许的值包括 unsorted, natural和某个实现了System.Collections.IComparer的类的名称
4.2 order-by
<map name="SortedList" order-by="elt asc">
order-by属性的值是一个SQL排序子句而不是HQL的
5.idbag
<class name="A" table="a" lazy="false"> <id name="Id" column="id" unsaved-value="null"> <generator class="native" /> </id> <property name="Name" column="aname" /> <idbag name="Items" cascade="all-delete-orphan" generic="true"> <collection-id type="Int32" column="item_id"> <generator class="increment" /> </collection-id> <key column="a_id" /> <element type="string" /> </idbag> </class>
与bag不同,多出了一个id字段,可以有效进行索引