简体   繁体   中英

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 from table2 p where c.fkidcolumme=p.idcolumme) in (:listPColummewhatever) ");

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

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();

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM