在 NHibernate 中使用子查询接口
我通常在 NHibernate 中使用 DetachedCriteria 查询接口:
DetachedCriteria crit = DetachedCriteria.For<IParent>();
这工作得很好。
我现在想为子对象创建一个子查询:
DetachedCriteria subcrit = DetachedCriteria.For<IChild>();
并将其添加到像这样的条件中(有点,p.Child实际上是一个别名,但我已经简化了):
crit.Add(LambdaSubquery.Property<IParent>(p => p.Child.ChildID).In(subcrit));
如果我的DeetchedCriteria 用于子级:
DetachedCriteria subcrit = DetachedCriteria.For<Child>();
但不是用于接口(如上所述)。在这种情况下,我得到一个例外:
NHibernate.MappingException: No persister for: Domain.Name.Space.IChild
这是本来就是这样的东西还是我缺少一些配置?
I normally query interfaces using DetachedCriteria in NHibernate:
DetachedCriteria crit = DetachedCriteria.For<IParent>();
And this works fine.
I now want to create a subquery for a child object thus:
DetachedCriteria subcrit = DetachedCriteria.For<IChild>();
and add it to the criteria like this (kind of, p.Child is actually an alias but I've simplified):
crit.Add(LambdaSubquery.Property<IParent>(p => p.Child.ChildID).In(subcrit));
This works if my DetchedCriteria is for a Child:
DetachedCriteria subcrit = DetachedCriteria.For<Child>();
but not it it's for the interface (as above). In that instance I get an exception:
NHibernate.MappingException: No persister for: Domain.Name.Space.IChild
Is this something that's meant to be or am I missing some config?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,我认为您无法查询接口,因为 NHibernate 将无法找到您要查询的正确实现类。例如,假设您有另一个名为 ChildLight 的 IChild 实现者(或映射到不同表的东西),NHibernate 没有指示要检索哪个实现类。
您需要创建一个 DetachedCriteria.For < Child >() 而不是接口。
I don't think you would be able to query against the interface in this case because NHibernate wouldn't be able to find the right implementing class you're intended to query against. For example, suppose you have another implementer of IChild called ChildLight (or something that is mapped to a different table), NHibernate has no direction for which implementing class to retrieve.
You need to create a DetachedCriteria.For < Child >() rather than the interface.