NHibernate Step By Step(7)-映射集合类型
来源:互联网 发布:金融网络销售装美女 编辑:程序博客网 时间:2024/06/05 10:13
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关系
(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字段,可以有效进行索引
- NHibernate Step By Step(7)-映射集合类型
- NHibernate Step By Step(2)-继承映射
- NHibernate Step by Step
- NHibernate Step by Step
- NHibernate Step by Step:序篇
- NHibernate Step by Step:序篇
- NHibernate Step by Step:序篇
- NHibernate Step by Step (一) Hello,NHibernate!
- NHibernate Step by Step (一) Hello,NHibernate!
- NHibernate Step by Step (一) Hello,NHibernate!
- NHibernate Step by Step (一) Hello,NHibernate!
- NHibernate Step by Step (一) Hello,NHibernate!
- NHibernate Step by Step (一) Hello,NHibernate!
- NHibernate Step by Step (一) Hello,NHibernate!
- NHibernate Step by Step (一) Hello,NHibernate!
- NHibernate Step by Step (一) Hello,NHibernate!
- NHibernate Step by Step (五)Criteria Query
- NHibernate Step by Step (五)Criteria Query
- Spring.NET学习笔记(5)-对象生命周期和创建者对象
- Spring.NET学习笔记(4)-对象作用域和类型转换
- Spring.NET学习笔记(3)-注册事件注入
- Spring.NET学习笔记(2)-依赖注入细节
- Spring.NET学习笔记(1)-基本依赖注入
- NHibernate Step By Step(7)-映射集合类型
- NHibernate Step By Step(6)-关联关系
- NHibernate Step By Step(5)-实体表现模式
- NHibernate Step By Step(4)-事件(Event)
- NHibernate Step By Step(3)-数据操作回调方法(拦截器)
- NHibernate Step By Step(2)-继承映射
- NHibernate Step By Step(1)-配置文件(Configuration)
- com学习笔记(10)自动化
- com学习笔记(9)IDL