关于NHibernate中one to many 的问题

最近研究NHibernate在多表映射方面遇到了一些问题,下面是我的具体代码:
1.首先建立两张表:parent和child,映射文件和Model类如下:
child.cs
 1 None.gif using  System;
 2 None.gif using  System.Collections;
 3 None.gif
 4 None.gif namespace  Test.Model
 5 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 6InBlock.gif    
 7InBlock.gif
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 9InBlock.gif    /// Child object for NHibernate mapped table 'child'.
10ExpandedSubBlockEnd.gif    /// </summary>

11InBlock.gif    public class Child
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        
14InBlock.gif        protected int _id;
15InBlock.gif        protected int _parentid;
16InBlock.gif        protected string _cname;
17InBlock.gif        protected Parent parent;
18InBlock.gif
19InBlock.gif        
20InBlock.gif
21ExpandedSubBlockStart.gifContractedSubBlock.gif        public Child() dot.gif{ }
22InBlock.gif
23InBlock.gif        public Child( int parentid, string cname)
24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
25InBlock.gif            this._parentid = parentid;
26InBlock.gif            this._cname = cname;
27ExpandedSubBlockEnd.gif        }

28InBlock.gif
29InBlock.gif
30InBlock.gif
31InBlock.gif        public int Id
32ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
33ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif{return _id;}
34ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{_id = value;}
35ExpandedSubBlockEnd.gif        }

36InBlock.gif
37InBlock.gif        public int Parentid
38ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
39ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _parentid; }
40ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ _parentid = value; }
41ExpandedSubBlockEnd.gif        }

42InBlock.gif
43InBlock.gif        public string Cname
44ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
45ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _cname; }
46InBlock.gif            set
47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
48InBlock.gif                if ( value != null && value.Length > 50)
49InBlock.gif                    throw new ArgumentOutOfRangeException("Invalid value for Cname", value, value.ToString());
50InBlock.gif                _cname = value;
51ExpandedSubBlockEnd.gif            }

52ExpandedSubBlockEnd.gif        }

53InBlock.gif
54InBlock.gif        public Parent Parent
55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
56ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn parent; }
57ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ parent = value; }
58ExpandedSubBlockEnd.gif        }

59InBlock.gif    
60InBlock.gif
61ExpandedSubBlockEnd.gif    }

62InBlock.gif    
63ExpandedBlockEnd.gif}

parent.cs
 1 None.gif using  System;
 2 None.gif using  System.Collections;
 3 None.gif using  Iesi.Collections;
 4 None.gif
 5 None.gif namespace  Test.Model
 6 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 7InBlock.gif
 8ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 9InBlock.gif    /// Parent object for NHibernate mapped table 'parent'.
10ExpandedSubBlockEnd.gif    /// </summary>

11InBlock.gif    public class Parent
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        
14InBlock.gif        protected int _id;
15InBlock.gif        protected string _pname;
16InBlock.gif        protected ISet childs = new HashedSet();
17InBlock.gif
18InBlock.gif
19InBlock.gif
20ExpandedSubBlockStart.gifContractedSubBlock.gif        public Parent() dot.gif{ }
21InBlock.gif
22InBlock.gif        public Parent( string pname)
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            this._pname = pname;
25ExpandedSubBlockEnd.gif        }

26InBlock.gif
27InBlock.gif
28InBlock.gif
29InBlock.gif        public int Id
30ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
31ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gif{return _id;}
32ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{_id = value;}
33ExpandedSubBlockEnd.gif        }

34InBlock.gif
35InBlock.gif        public string Pname
36ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
37ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn _pname; }
38InBlock.gif            set
39ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
40InBlock.gif                if ( value != null && value.Length > 50)
41InBlock.gif                    throw new ArgumentOutOfRangeException("Invalid value for Pname", value, value.ToString());
42InBlock.gif                _pname = value;
43ExpandedSubBlockEnd.gif            }

44ExpandedSubBlockEnd.gif        }

45InBlock.gif
46InBlock.gif        public ISet Childs
47ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
48ExpandedSubBlockStart.gifContractedSubBlock.gif            get dot.gifreturn childs; }
49ExpandedSubBlockStart.gifContractedSubBlock.gif            set dot.gif{ childs = value; }
50ExpandedSubBlockEnd.gif        }

51InBlock.gif
52InBlock.gif
53ExpandedSubBlockEnd.gif    }

54ExpandedBlockEnd.gif}

child.hbm.xml:
 1 None.gif <? xml version="1.0" encoding="utf-8"  ?>
 2 None.gif < hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.0" >
 3 None.gif     < class  name ="Test.Model.Child, Test.Model"  table ="child" >
 4 None.gif         < id  name ="Id"  type ="Int32"  unsaved-value ="null" >
 5 None.gif             < column  name ="child_id"  length ="4"  sql-type ="int"  not-null ="true"  unique ="true"  index ="PK_child" />
 6 None.gif             < generator  class ="native"   />
 7 None.gif         </ id >
 8 None.gif     < many-to-one
 9 None.gif       name ="Parent"
10 None.gif      column ="parent_id"
11 None.gif      class ="Test.Model.Parent,Test.Model"
12 None.gif      unique ="true"
13 None.gif     />
14 None.gif     < property  name ="Cname"  type ="String" >
15 None.gif             < column  name ="cname"  length ="50"  sql-type ="varchar"  not-null ="false" />
16 None.gif         </ property >
17 None.gif     </ class >
18 None.gif </ hibernate-mapping >

parent.hbm.xml:

 1 None.gif <? xml version="1.0" encoding="utf-8"  ?>
 2 None.gif < hibernate-mapping  xmlns ="urn:nhibernate-mapping-2.0" >
 3 None.gif     < class  name ="Test.Model.Parent,Test.Model"  table ="parent" >
 4 None.gif         < id  name ="Id"  type ="Int32"  unsaved-value ="null" >
 5 None.gif             < column  name ="parent_id"  length ="4"  sql-type ="int"  not-null ="true"  unique ="true"  index ="PK_parent" />
 6 None.gif             < generator  class ="native"   />
 7 None.gif         </ id >
 8 None.gif     < set  name ="Childs"  cascade ="all"  inverse ="true"  lazy ="false" >
 9 None.gif       < key  column ="parent_id"   />
10 None.gif       < one-to-many  class ="Test.Model.Child, Test.Model"   />
11 None.gif     </ set >
12 None.gif         < property  name ="Pname"  type ="String" >
13 None.gif             < column  name ="pname"  length ="50"  sql-type ="varchar"  not-null ="false" />
14 None.gif         </ property >
15 None.gif   </ class >
16 None.gif </ hibernate-mapping >

配置好了以后,我做如下操作:

 1 None.gif using  System;
 2 None.gif using  System.Collections.Generic;
 3 None.gif using  System.Text;
 4 None.gif using  NHibernate;
 5 None.gif using  NHibernate.Cfg;
 6 None.gif using  Test.Model;
 7 None.gif using  Iesi.Collections;
 8 None.gif
 9 None.gif namespace  Console1
10 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
11InBlock.gif    class Program
12ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
13InBlock.gif        static void Main(string[] args)
14ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
15InBlock.gif            Configuration config = new Configuration().AddAssembly("Test.Model");
16InBlock.gif
17InBlock.gif            ISessionFactory sessionFactory = config.BuildSessionFactory();
18InBlock.gif
19InBlock.gif            ISession session = sessionFactory.OpenSession();
20InBlock.gif
21InBlock.gif            try
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif
24InBlock.gif                Parent parent = new Parent();
25InBlock.gif                parent.Pname = "pw";
26InBlock.gif
27InBlock.gif                Child child = new Child();
28InBlock.gif                child.Cname = "child of pw";
29InBlock.gif                child.Parent = parent;
30InBlock.gif
31InBlock.gif                parent.Childs.Add(child);
32InBlock.gif
33InBlock.gif                session.Save(parent);
34ExpandedSubBlockEnd.gif            }

35InBlock.gif            catch (Exception e)
36ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
37InBlock.gif                throw new ApplicationException(e.Message);
38ExpandedSubBlockEnd.gif            }

39ExpandedSubBlockEnd.gif        }

40ExpandedSubBlockEnd.gif    }

41ExpandedBlockEnd.gif}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值