Nhibernate 是否可以为基元集合上的分离条件创建限制?
我想知道是否有办法在NHibernate.3.3中对模型的原始集合创建限制 详情如下:Nhibernate 是否可以为基元集合上的分离条件创建限制?,nhibernate,primitive,detachedcriteria,restrictions,Nhibernate,Primitive,Detachedcriteria,Restrictions,我想知道是否有办法在NHibernate.3.3中对模型的原始集合创建限制 详情如下: class Parent { IEnumerable<string> ChildNames { get; set; } } 类父{ IEnumerable ChildNames{get;set;} } 我需要这样搜索: private DetachedCriteria BuildQuery() { var inNames = { "Bob", "Sam", "Dan" };
class Parent {
IEnumerable<string> ChildNames { get; set; }
}
类父{
IEnumerable ChildNames{get;set;}
}
我需要这样搜索:
private DetachedCriteria BuildQuery() {
var inNames = { "Bob", "Sam", "Dan" };
var query = DetachedCriteria.For<Parent>("parent");
query.Add(Restrictions.In("ChildNames", inNames));
return query;
}
private DetachedCriteria BuildQuery(){
var inNames={“Bob”、“Sam”、“Dan”};
var query=DetachedCriteria.For(“父项”);
query.Add(Restrictions.In(“ChildNames”,inNames));
返回查询;
}
我发现这个老问题说这是不可能的,但考虑到它很老而且没有太多的投票权,我想在重构之前确认一下
如果我能做到,而且我完全搞砸了,我也会接受帮助 在这个场景中,我们可以使用
投影
(某种类型不太安全的东西,然后是映射属性,但更灵活)
让我们期待这样的映射:
<bag name="ChildNames" inverse="false" lazy="true" table="[dbo].[ChildNames]"
cascade="all"
batch-size="25">
<key column="ParentId" />
<element type="System.String" column="ChildName" />
</bag>
protected virtual DetachedCriteria BuildQuery()
{
var inNames = new [] { "Bob", "Sam", "Dan" };
// parent query reference
var query = DetachedCriteria.For<Parent>("parent");
// reference to child query
var child = query.CreateCriteria("ChildNames");
// let's project the column name of the Element, e.g. 'Name'
var columnNameProjection = Projections.SqlProjection(
"ChildName as name", null, new IType[] { NHibernateUtil.String }
);
// in clause
child.Add(Restrictions.In(
columnNameProjection, inNames
));
return query;
}
警告:
虽然这确实有效。。。使用
ChildName
时不带别名。这可能很难实现。。。所以要小心,如果在这个场景中有更多名为ChildName
的列我最终像许多人一样,将集合重构为强类型。使用in子句限制检查[this][1]。[1]:
SELECT ...
FROM Parent this_
inner join [dbo].[ChildNames] childNames3_
on this_.ParentId=childNames3_.ParentId
WHERE ChildName in (@p0, @p1, @p2)
...
@p0=N'Bob',@p1=N'Sam',@p2=N'Dan'