How to write a Criteria Query when there's an <any> association
Posted
by Bevan
on Stack Overflow
See other posts from Stack Overflow
or by Bevan
Published on 2010-04-30T04:34:42Z
Indexed on
2010/04/30
4:47 UTC
Read the original article
Hit count: 300
nhibernate
|criteria
I'm having some trouble constructing the correct Criteria to do a particular query - after an afternoon of consultation with Professor Google, I'm hoping that someone can point me in the right direction.
I have two entities of interest: OutputTsDef
and NamedAttribute
What I'm trying to do is to find all OutputTsDef
that have a particular NamedAttribute
value.
I can write a detached Criteria to find all NamedAttributes
that have
a given name and value:
var attributesCriteria
= DetachedCriteria.For<INamedAttribute>()
.Add(Expression.Eq("Name", "some name"))
.Add(Expression.Eq("Value", "some value"));
How do I inject this in to a query for OutputTsDef to restrict the results?
var criteria
= nHibernateSession.CreateCriteria(typeof(IOutputTsDefEntity));
// What do I write here?
var results = criteria.List();
NamedAttribute
looks like this - note the use of [Any]
as we can have
NamedAttributes
on many kinds of entity.
[AttributeIdentifier("DbKey", Name = "Id.Column", Value = "NamedAttributeID")]
[Class(Table = "NamedAttributes")]
public class NamedAttribute : BusinessEntity, INamedAttribute
{
[Any(0, Name = "Entity", MetaType = "System.String", IdType = "System.Int32")]
[MetaValue(1, Class = "Sample.OutputTsDef, Sample.Entities", Value = "OTD")]
[MetaValue(2, Class = "Sample.OutputTimeSeriesAttributesEntity, Sample.Entities", Value = "OTA")]
[Column(3, Name = "OwnerType")]
[Column(4, Name = "OwnerKey")]
public virtual IBusinessEntity Entity { get; set; }
[Property(Column = "Name")]
public virtual string Name { get; set; }
[Property(Column = "Value")]
public virtual string Value { get; set; }
... omitted ...
}
In regular SQL, I'd just include an extra "where" clause like this:
where OutputTsDefId
in ( select distinct OwnerKey
from NamedAttributes
where Name = ?
and Value = ?
and OwnerType = 'OTD' )
What am I missing?
(Question also posted to the NHUsers mailing list - I'll copy any useful information from there, here.)
© Stack Overflow or respective owner