1

私はHQL(一般的にはnHibernate)にかなり慣れておらず、現在、HQLについてさらに学ぶための簡単なアプリを構築しています。

ただし、次のSQLをHQLとして表現しようとすると問題が発生しますが、アイデアがあれば非常にありがたいです。

クエリは次のとおりです。

select * from parent p
where p.id in (select p.parentid from child c where c.createdby = 2)
and
(select top 1 createdby 
 from child where parentid = p.id order by createdon desc) != 2
4

2 に答える 2

0

ありがとう-それは私を正しい軌道に乗せました。「Top」は使用できませんでしたが、このようにクエリを書き直すと、うまくいったようです。

select p from Parent p where p.ID in 
  (select c.parent.Id from Child c where c.CreatedBy = "+user.ID+") 
   and "+ user.ID +" = (select max(c.CreatedBy) 
from Child c 
where child.parent.ID = parent.ID

厄介な文字列の連結を失礼します-次のステップは、パラメータ化されたHQLを使用してこれをクリーンアップすることです!

于 2008-11-10T00:25:17.487 に答える
0

2番目の部分を保証することはできませんが、おそらくこれで目標に近づくことができます. 多対 1 参照の parentid 比較を置き換えました。いずれも hql で動作するはずです。

select p from parent p 
    where p in (select c.ParentReference from child c 
        where c.createdby = :twoparameter)
    and :twoparameter = (select top 1 c.createdby from child 
        where c.ParentReference = p order by p.createdon desc)
于 2008-11-09T15:52:35.743 に答える