1.首先建立两张表:parent和child,映射文件和Model类如下:
child.cs
1
using System;
2
using System.Collections;
3
4
namespace Test.Model
5
}
2
3
4
5
parent.cs
1
using System;
2
using System.Collections;
3
using Iesi.Collections;
4
5
namespace Test.Model
6
}
2
3
4
5
6
child.hbm.xml:
1
<?xml version="1.0" encoding="utf-8" ?>
2
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
3
<class name="Test.Model.Child, Test.Model" table="child">
4
<id name="Id" type="Int32" unsaved-value="null">
5
<column name="child_id" length="4" sql-type="int" not-null="true" unique="true" index="PK_child"/>
6
<generator class="native" />
7
</id>
8
<many-to-one
9
name="Parent"
10
column="parent_id"
11
class="Test.Model.Parent,Test.Model"
12
unique="true"
13
/>
14
<property name="Cname" type="String">
15
<column name="cname" length="50" sql-type="varchar" not-null="false"/>
16
</property>
17
</class>
18
</hibernate-mapping>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
parent.hbm.xml:
1
<?xml version="1.0" encoding="utf-8" ?>
2
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
3
<class name="Test.Model.Parent,Test.Model" table="parent">
4
<id name="Id" type="Int32" unsaved-value="null">
5
<column name="parent_id" length="4" sql-type="int" not-null="true" unique="true" index="PK_parent"/>
6
<generator class="native" />
7
</id>
8
<set name="Childs" cascade="all" inverse="true" lazy="false">
9
<key column="parent_id" />
10
<one-to-many class="Test.Model.Child, Test.Model" />
11
</set>
12
<property name="Pname" type="String">
13
<column name="pname" length="50" sql-type="varchar" not-null="false"/>
14
</property>
15
</class>
16
</hibernate-mapping>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
配置好了以后,我做如下操作:
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using NHibernate;
5
using NHibernate.Cfg;
6
using Test.Model;
7
using Iesi.Collections;
8
9
namespace Console1
10
}
2
3
4
5
6
7
8
9
10
然后运行总是会提示如下错误:“Unkown Entity Class :Test.Model.Parent”,我苦思一个晚上也没发现问题的所在,哪位高手能指点一下?感激不尽阿!