1.首先建立两张表:parent和child,映射文件和Model类如下:
child.cs
1
using System;
2
using System.Collections;
3
4
namespace Test.Model
5
{
6
7
8
/// <summary>
9
/// Child object for NHibernate mapped table 'child'.
10
/// </summary>
11
public class Child
12
{
13
14
protected int _id;
15
protected int _parentid;
16
protected string _cname;
17
protected Parent parent;
18
19
20
21
public Child() { }
22
23
public Child( int parentid, string cname)
24
{
25
this._parentid = parentid;
26
this._cname = cname;
27
}
28
29
30
31
public int Id
32
{
33
get {return _id;}
34
set {_id = value;}
35
}
36
37
public int Parentid
38
{
39
get { return _parentid; }
40
set { _parentid = value; }
41
}
42
43
public string Cname
44
{
45
get { return _cname; }
46
set
47
{
48
if ( value != null && value.Length > 50)
49
throw new ArgumentOutOfRangeException("Invalid value for Cname", value, value.ToString());
50
_cname = value;
51
}
52
}
53
54
public Parent Parent
55
{
56
get { return parent; }
57
set { parent = value; }
58
}
59
60
61
}
62
63
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

parent.cs
1
using System;
2
using System.Collections;
3
using Iesi.Collections;
4
5
namespace Test.Model
6
{
7
8
/// <summary>
9
/// Parent object for NHibernate mapped table 'parent'.
10
/// </summary>
11
public class Parent
12
{
13
14
protected int _id;
15
protected string _pname;
16
protected ISet childs = new HashedSet();
17
18
19
20
public Parent() { }
21
22
public Parent( string pname)
23
{
24
this._pname = pname;
25
}
26
27
28
29
public int Id
30
{
31
get {return _id;}
32
set {_id = value;}
33
}
34
35
public string Pname
36
{
37
get { return _pname; }
38
set
39
{
40
if ( value != null && value.Length > 50)
41
throw new ArgumentOutOfRangeException("Invalid value for Pname", value, value.ToString());
42
_pname = value;
43
}
44
}
45
46
public ISet Childs
47
{
48
get { return childs; }
49
set { childs = value; }
50
}
51
52
53
}
54
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

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
{
11
class Program
12
{
13
static void Main(string[] args)
14
{
15
Configuration config = new Configuration().AddAssembly("Test.Model");
16
17
ISessionFactory sessionFactory = config.BuildSessionFactory();
18
19
ISession session = sessionFactory.OpenSession();
20
21
try
22
{
23
24
Parent parent = new Parent();
25
parent.Pname = "pw";
26
27
Child child = new Child();
28
child.Cname = "child of pw";
29
child.Parent = parent;
30
31
parent.Childs.Add(child);
32
33
session.Save(parent);
34
}
35
catch (Exception e)
36
{
37
throw new ApplicationException(e.Message);
38
}
39
}
40
}
41
}

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

然后运行总是会提示如下错误:“Unkown Entity Class :Test.Model.Parent”,我苦思一个晚上也没发现问题的所在,哪位高手能指点一下?感激不尽阿!