开发者

Converting HQL 'in' restriction to Criteria

I have a query like this:

Query query = session.createQuery("from table1 c where c.colummewhatever =:value and (select p.colummewhatever fr开发者_如何学编程om table2 p where c.fkidcolumme=p.idcolumme) in (:listPColummewhatever) ");

Is there a way to translate the in restriction into Criteria?


It seems to me that this query could be rewritten using a simple join:

from Table1 c inner join c.table2 p
where c.colummewhatever =:value 
and p.colummewhatever in (:listPColummewhatever)

Translating this HQL query in Criteria is now much easier.

This of course supposes that you have an association between Table1 and Table2, but you should have one.


Using the Criteria API should be somethin like this...

DetachedCriteria dc = new DetachedCriteria.forClass(Table1.class,"c");
dc.createAlias("table2","p")
dc.add(Restrictions.in("p.colFoo",yourListFoo);
dc.add(Restrictions.eq("c.col",value);

List<Table1> = dc.getExecutableCriteria(session).list();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜