无法将查询转换为HQL

时间:2021-06-26 22:25:31

I'm pretty new to HQL (well, nHibernate in general) and am currently building a simple app to learn more about it.

我是HQL的新手(嗯,nHibernate一般),我正在构建一个简单的应用程序来了解它。

I've run into problems trying to express the following SQL as HQL though, and would be very grateful for any ideas.

我试图将以下SQL表示为HQL时遇到了问题,并且非常感谢任何想法。

Here's the query:

这是查询:

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

2 个解决方案

#1


0  

Can't vouch for the second part but perhaps this can get you closer to the goal. I replaced the the parentid comparisons for a many-to-one reference. Any should work in hql.

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

#2


0  

Thanks - that got me on the right track. I couldn't use "Top" but rewriting the query like this seems to have done the trick:

谢谢 - 这让我走上正轨。我不能使用“顶部”,但重写这样的查询似乎已经完成了诀窍:

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

Excuse the nasty string concatenation - next step is to clean this up using parameterized HQL!

请原谅令人讨厌的字符串连接 - 下一步是使用参数化的HQL清理它!

#1


0  

Can't vouch for the second part but perhaps this can get you closer to the goal. I replaced the the parentid comparisons for a many-to-one reference. Any should work in hql.

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

#2


0  

Thanks - that got me on the right track. I couldn't use "Top" but rewriting the query like this seems to have done the trick:

谢谢 - 这让我走上正轨。我不能使用“顶部”,但重写这样的查询似乎已经完成了诀窍:

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

Excuse the nasty string concatenation - next step is to clean this up using parameterized HQL!

请原谅令人讨厌的字符串连接 - 下一步是使用参数化的HQL清理它!