我有一个“地产”实体,并且该实体具有集“EstateFeatures”(类型:EstateFeature)。和EstateFeature具有属性“MyFeatureValue”

注意:这些是问题的有限的性能。所有实体具有ID和所有necesarry等

<强>村

IList<EstateFeature> EstateFeatures;

<强> EstateFeature

FeatureValue MyFeatureValue;

<强> FeatureValue

public virtual long Id;

我想获得性房地产已经给定FeatureValue.Id

DetachedCriteria query = DetachedCriteria.For<Estate>();
Conjunction and = new Conjuction();
foreach (var id in idCollection)
   and.Add(Expression.Eq("MyFeatureValue.Id",id);

query
     .CreateCriteria("EstateFeatures")
     .Add(and);
IList<Estate> estates = query.GetExecutableCriteria(session).List<Estate>();

没有任何来自该查询返回的,我是不是做错了什么?

由于

有帮助吗?

解决方案

您需要确保您加入MyFeatureValue一个时间,你希望你的房地产有各自的特征。

的一种方法是调用.CreateAlias对于每次迭代,给它一个独特的别名然后添加表述“aliasX.Id”

foreach (var id in idCollection)
{
   query = query.CreateAlias("MyFeatureValue", "feature" + id)
                .Add(Expression.Eq("feature" + id + ".Id",id);


}

并没有真正记得的语法如何去,写了这出我的头,不知道你是否需要重新声明查询之一:)

不过,我认为这将让你开始。

编辑:由于标准API中的错误使用CreateAlias或个createCriteria多次关联集合约束你,你需要求助于的HQL

HTTP://德里克-说.blogspot.com / 2008/06 /复制关联路径-bug的in.html

(休眠从相同的问题藏汉遭受)

select e   
FROM Estate AS e
INNER JOIN e.MyFeatureValue AS fv1
INNER JOIN e.MyFeatureValue AS fv2
WHERE fv1.Id = 3
   AND fv2.Id = 13

您将需要动态地建立HQL,使您的别名成为唯一的(FV1,FV2,FVX ...)

其他提示

如果我理解正确的话,我认为这样的事情可能会奏效

CreateCriteria(typeof(Estate))
     .CreateAlias("EstateFeatures", "estatefeature")
     .Add(Restrictions.In("estatefeature.MyFeatureValue.Id", ids))
     .List<Estate>();

没有NHibernate的为您生成查询什么?您可以通过使用show_sql config属性检查。

当我看到您的查询,你正在试图获得具有给定的功能集所有遗产。 我认为,这将生成一个查询,看起来像

SELECT ....
FROM Estates
INNER JOIN Features
WHERE Feature.Id = 1 AND Feature.Id = 2 ...

如果要检索包含所有指定功能的所有财产,我认为你将不得不使用一个析取,让NHibernate的检索具有这些特征的至少一个所有遗产。 然后,在你的客户端代码,你必须检查每个村在你的“客户端代码”,让你最终刚刚结束了与遗产有所有功能。结果 我不知道是否有让NHibernate的处理这个问题的有效途径......

代码看起来像你传递FeaturesValueIds的列表,并想拥有所有这些功能的列表。如果是这样的话,我想看一看正在生成的SQL,并运行它针对数据库,看看你是否应该得到带回点什么。

另外,如果你正在寻找有什么要传递的功能的列表,你应该使用一个析取,而不是一个连词。

    exec sp_executesql N'SELECT TOP 3 id11_1_, Address11_1_, Title11_1_, Descript4_11_1_, 
    Price11_1_, Discount11_1_, ForBankL7_11_1_, AddDate11_1_, LastUpdate11_1_, 
IsVisible11_1_, ViewCount11_1_, SaleOrRent11_1_, LocationId11_1_, StaffId11_1_, 
CategoryId11_1_, id27_0_, EstateId27_0_, FeatureV3_27_0_ FROM (SELECT ROW_NUMBER() 
OVER(ORDER BY __hibernate_sort_expr_0__) as row, query.id11_1_, query.Address11_1_, 
query.Title11_1_, query.Descript4_11_1_, query.Price11_1_, query.Discount11_1_, 
query.ForBankL7_11_1_, query.AddDate11_1_, query.LastUpdate11_1_, query.IsVisible11_1_, 
query.ViewCount11_1_, query.SaleOrRent11_1_, query.LocationId11_1_, query.StaffId11_1_, 
query.CategoryId11_1_, query.id27_0_, query.EstateId27_0_, query.FeatureV3_27_0_, 
query.__hibernate_sort_expr_0__ FROM (SELECT this_.id as id11_1_, this_.Address as 
Address11_1_, this_.Title as Title11_1_, this_.Description as Descript4_11_1_, this_.Price 
as Price11_1_, this_.Discount as Discount11_1_, this_.ForBankLoan as ForBankL7_11_1_, 
this_.AddDate as AddDate11_1_, this_.LastUpdate as LastUpdate11_1_, this_.IsVisible as 
IsVisible11_1_, this_.ViewCount as ViewCount11_1_, this_.SaleOrRent as SaleOrRent11_1_, 
this_.LocationId as LocationId11_1_, this_.StaffId as StaffId11_1_, this_.CategoryId as 
CategoryId11_1_, estatefeat1_.id as id27_0_, estatefeat1_.EstateId as EstateId27_0_, 
estatefeat1_.FeatureValueId as FeatureV3_27_0_, CURRENT_TIMESTAMP as 
__hibernate_sort_expr_0__ FROM Estate this_ inner join EstateFeature estatefeat1_ on 
this_.id=estatefeat1_.EstateId WHERE this_.CategoryId = @p0 and 
(estatefeat1_.FeatureValueId = @p1 and estatefeat1_.FeatureValueId = @p2 and 
estatefeat1_.FeatureValueId = @p3 and estatefeat1_.FeatureValueId = @p4 and 
estatefeat1_.FeatureValueId = @p5 and estatefeat1_.FeatureValueId = @p6 and 
estatefeat1_.FeatureValueId = @p7)) query ) page WHERE page.row > 0 ORDER BY 
__hibernate_sort_expr_0__',N'@p0 bigint,@p1 bigint,@p2 bigint,@p3 bigint,@p4 bigint,@p5 
bigint,@p6 bigint,@p7 bigint',@p0=3,@p1=7,@p2=8,@p3=9,@p4=10,@p5=11,@p6=12,@p7=16

它看起来像你想orDisjunction),而不是andConjunction)。现在,您正在搜索的对象EstateFeatures使得每个对象有多个不同的Ids,这似乎不是你想要的。

var or = new Disjunction();
foreach(var id in idCollection)
    or.Add(Expression.Eq("MyFeatureValue.Id", id);

var query = DetachedCriteria.For<Estate>();
query
    .CreateCriteria("EstateFeatures")
    .Add(and);
var estates = query.GetExecutableCriteria(session).List<Estate>();

我也试过,但结果是相同的:

DetachedCriteria features = DetachedCriteria.For<FeatureValue>();
features.SetProjection(Projections.Property("Id"));
features.Add(Property.ForName("Id").EqProperty("value.Id"));

var and = new Conjunction();

foreach (var l in FeatureIdCollection)
    and.Add(Expression.Eq("Id", l));

features.Add(and);

query.CreateCriteria("EstateFeatures")
     .CreateCriteria("MyFeatureValue","value")
     .Add(Subqueries.Exists(features));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top