照会にコレクションの設定によりどちらのモ基準Api?
-
22-08-2019 - |
質問
私は"不動産"企業は、このエンティティはコレクション"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); }
だん記憶がどのよう構文があり、"この私の頭のいが必要な場合redeclareクエリのいずれか)
しかしあまり多くなかったように思います。
編集:以来、バグの基準APIを抑制すから関連付けを集め、複数回使用CreateAliasはCreateCriteria必要がありリゾートにはHQL.
http://derek-says.blogspot.com/2008/06/duplicate-association-path-bug-in.html
(Hibernateになるとともに、同誌掲載号の注目レガントなホテルでした。)
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>();
どのクエリかったの設定によりどちらのモを生成しですか?確認することができ用のshow_sql config物件です。
私のみ検索すると、まることができるようすべてのエスが与えられたのが特徴です。と思い、この生まれるクエリのように見え
SELECT ....
FROM Estates
INNER JOIN Features
WHERE Feature.Id = 1 AND Feature.Id = 2 ...
したい場合に取得するすべての団地を含む定されているすべての特徴と思いますので利用し、分離し、その設定によりどちらのモを取得しますすべての団地として少なくとも一つの特徴です。そして、クライアントコードの高い検査毎に自分自身やクライアントコード'は、それだけで終わ団地しています。
わからない場合があり効率的な方法のう設定によりどちらのモ扱うこ---
次のようなコードになりま通のリスト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
このように見えたい or
(Disjunction
)の代わりに and
(Conjunction
).現在、探している EstateFeature
s物はそれぞれ、そのオブジェクトが複数の異なる Id
sかせないと考えている。
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));