1

我对 HQL 很陌生(嗯,一般是 nHibernate),目前正在构建一个简单的应用程序来了解更多信息。

我在尝试将以下 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

不能保证第二部分,但也许这可以让你更接近目标。我将 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 回答