开发者

Using an interface for a subquery in NHibernate

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<I开发者_如何转开发Child>();

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜