Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top