Nhibernate 外键作为复合键
您好,我对 Nhibernate 很陌生,我正在开发一个项目,描述如下:
1 “Parents”表,包含 ParentName 和使用 IList 的一对多 ChildrenList,主键设置为 ParentId
1 “Children”表,带有一个包含 ParentID 作为键之一的复合主键,
因此我在 Parent.hbm.xml 中设置了一对多,如下所示
<class name="ParentName " table="Parents">
<id name="ParentName " column="ParentName ">
<generator class="assigned"/>
</id>
<property name="ParentAlpha" />
<bag name="ChildrenList" cascade="all">
<key column="ParentName" />
<one-to-many class="Children"/>
</bag>
,在 Children.hbm.xml 中设置了如下
<composite-id>
<key-many-to-one name="ParentName" class="Parent"/>
<key-property name = "ChildrenAlpha" />
<key-property name = "ChildrenAlpha2"/>
</composite-id>
<property name="ChildrenBeta" />
<property name="ChildrenGama" />
当前我正在使用一些子列表保存父对象进行测试使用 session.SaveOrUpdate 方法插入 MySQL 数据库,但它总是失败,只是说“无法插入”子对象
这是我的测试代码:
Parent parent = new Parent(){ParentAlpha= "ABC"};
Children children = new Children(){ChildrenAlpha = "AAA" ,ChildrenAlpha2 ="VBB"};
parent .ChildrenList.add(children); //IList add function
.....session.SaveOrUpdate(parent);
我已经测试了一个父对象到多个子对象,并且子对象主键设置为生成的 ChildrenId。这很好用。但不知怎的,我不能使用复合键来做到这一点,我的猜测是,因为 Children 中的 ParentName 是主键,但只有在 Parent 中的 ParentName 填充后才会填充,就像这样。
另一个问题是,如果上述问题解决了,我可以用它来检索整个父对象和子对象列表吗? (我尝试过简单的单一 PK 案例,但当孩子是复合关键事物时似乎不起作用)
Parent parent= session.CreateCriteria(typeof(Parent))
.Add(Restrictions.Eq("ParentName", ParentName))
.UniqueResult<Parent>();
NHibernateUtil.Initialize(parent.ChildrenList);
谢谢!
Hi I am pretty new to Nhibernate and I am working on a project that is described as follow:
1 "Parents" table containing ParentName and a one to many ChildrenList using IList, with Primary Key set to ParentId
1 "Children" table , with a composite primary key that contains ParentID as one of the key
so I set up a one to many in Parents.hbm.xml like this
<class name="ParentName " table="Parents">
<id name="ParentName " column="ParentName ">
<generator class="assigned"/>
</id>
<property name="ParentAlpha" />
<bag name="ChildrenList" cascade="all">
<key column="ParentName" />
<one-to-many class="Children"/>
</bag>
and the Children.hbm.xml like this
<composite-id>
<key-many-to-one name="ParentName" class="Parent"/>
<key-property name = "ChildrenAlpha" />
<key-property name = "ChildrenAlpha2"/>
</composite-id>
<property name="ChildrenBeta" />
<property name="ChildrenGama" />
And currently im doing test on saving Parent obj with some list of Children in to a MySQL database using the session.SaveOrUpdate method, but it always fails and just said "cannot insert "the Children Obj
Here is my test code:
Parent parent = new Parent(){ParentAlpha= "ABC"};
Children children = new Children(){ChildrenAlpha = "AAA" ,ChildrenAlpha2 ="VBB"};
parent .ChildrenList.add(children); //IList add function
.....session.SaveOrUpdate(parent);
I have tested for one parent to many children, and the children primary key is set to a generated ChildrenId. and this works fine. But somehow I cannot do it using composite key, my guess is that as ParentName in Children is a primary key but will only be filled after ParentName in Parent has been filled, well sth like that.
Another Question is that if the above problem is solved, can i use this to retrieve the whole parent obj with the children list? ( I tried it for simple single PK case but seems not working when children is a composite key thing)
Parent parent= session.CreateCriteria(typeof(Parent))
.Add(Restrictions.Eq("ParentName", ParentName))
.UniqueResult<Parent>();
NHibernateUtil.Initialize(parent.ChildrenList);
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论