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.)
Do the answers to this apparently similar hibernate question help?
This is what I ended up doing - embedding the SQL subquery check in this way:
const string subquery
= "{alias}.OutputTsDefId in "
+"( select OwnerKey "
+ " from NamedAttributes na "
+ " where na.Name = ? and na.Value = ? and OwnerType='OTD')";
criteria.Add(
Expression.Sql(
subquery,
new object[] { attributeFilter.Name, attributeFilter.Value },
new IType[] { NHibernateUtil.String, NHibernateUtil.String }));
This isn't ideal - I don't really like to tunnel past NHibernate in this way. But, it gets the job done, which counts for a lot.
I'm still interested in finding a pure NHibernate solution, if there is one.
精彩评论