Is it possible for this query to be used using criteria or hibernation

The question is simple, can this query be executed in Hibernate using criteria or DetachedCriteria? I think not, but I wanted to make this question, maybe there is a workaround.

SELECT COLUMNS FROM table WHERE id not in ( SELECT * FROM ( SELECT id FROM table WHERE SOMECONDITIONS ORDER BY timestamp desc limit 0, 15 ) as t); 

I mark @Dean Clark's answer as correct, but another question arises:

 where can i find the findByCriteria from SessionFactory we are not using Spring 
+5
source share
1 answer

To exactly match the query, you really need to do this with two steps, but I would avoid this if it was possible:

 final Criteria innerCriteria = getSession().createCriteria(YourEntity.class); // SOME CONDITIONS innerCriteria.add(Restrictions.eq("someColumn", "someValue")); innerCriteria.addOrder(Order.desc("timestamp")); innerCriteria.setMaxResults(15); innerCriteria.setProjection(Projections.id()); List<YourIdClass> ids = innerCriteria.list(); final Criteria criteria = getSession().createCriteria(YourEntity.class); criteria.add(Restrictions.not(Restrictions.in("id", ids))); List<YourEntity> results = criteria.list(); 

Will the objects you are trying to identify have the same "SOMECONDITIONS"? If so, it will functionally fulfill what you are looking for:

 final DetachedCriteria criteria = DetachedCriteria.forClass(YourEntity.class); // SOME CONDITIONS criteria.add(Restrictions.eq("someColumn", "someValue")); criteria.addOrder(Order.desc("timestamp")); getHibernateTemplate().findByCriteria(criteria, 16, 9999999); 
+6
source

Source: https://habr.com/ru/post/1265782/


All Articles