NHibernate组件与父项的一对多关系 [英] NHibernate component with a one-to-many relation from parent
本文介绍了NHibernate组件与父项的一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
lockquote
Queue.Id
使用Fluent NHibernate,将它映射到Queue类中的一个属性是非常简单的,即
/ * QueueMap * /
HasMany(x => x.Jobs)
.KeyColumnNames.Add(QueueId);
但是,假设我有一个非常好的理由来介绍一个类,例如:
public class Queue
{
public Group Group {get;组; }
}
public class Group
{
public IList< Job>工作{get;私人设置然后我需要使用一个Component来映射它,也就是
$ b $ / * QueueMap * /
组件(
x => x.Group,
y => y.HasMany(x => x.Jobs).KeyColumnNames.Add(QueueId)
);
当我这样做时,我得到以下内容:
{无法初始化集合:
[Queue.Group.Jobs#832fc413-c282-48e8-8cb6-d2a70b0b8de4]
[SQL:SELECT values0_ .QueueId QueueId1_,values0_.Id as Id1_,values0_.Id
as Id16_0_,(....)FROM dbo.Jobs values0_ WHERE values0_.QueueId =?]}
$ c $
解决方案解决了。这是由JobMap中的映射问题引起的。
Say I have a Queue table and a Job table. In the Job table there is a foreign key column QueueId for the Queue table, i.e.
Queue.Id <-- Job.QueueId
Using Fluent NHibernate it is pretty straightforward to map this to a property in the Queue class, i.e.
/* QueueMap */
HasMany(x => x.Jobs)
.KeyColumnNames.Add("QueueId");
But assume I have a very good reason to have a class inbetween, say something like:
public class Queue
{
public Group Group { get; set; }
}
public class Group
{
public IList<Job> Jobs { get; private set; }
}
Then I need to map this using a Component, i.e.
/* QueueMap */
Component(
x => x.Group,
y => y.HasMany(x => x.Jobs).KeyColumnNames.Add("QueueId")
);
When I do this I get the following:
{"could not initialize a collection:
[Queue.Group.Jobs#832fc413-c282-48e8-8cb6-d2a70b0b8de4]
[SQL: SELECT values0_.QueueId as QueueId1_, values0_.Id as Id1_, values0_.Id
as Id16_0_, (....) FROM dbo.Jobs values0_ WHERE values0_.QueueId=?]"}
Any idea as to what I'm doing wrong...
解决方案 Solved. This was caused by a mapping problem in the JobMap.
这篇关于NHibernate组件与父项的一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文