What is the best way to use hibernate’s detachedcriteria to limit results in Java?
•
Java
I use hibernate 3.5.0 in Java 6-Final. Because I can't access hibernate session, I use detachedcriteria So, I want to know what is the best way to limit the results of detachedcriteria (in my case, I want to get only the first row)
Additional information:
Criteria class has some methods to achieve this, such as setmaxresults (int maxresults) or setfirstresult (int firstresult), but detachedcriteria does not Similarly, I can't use criteria because I can't access hibernate's session
Solution
That's what I'm doing. You have to wrap your results into execution blocks
final DetachedCriteria criteria = DetachedCriteria.forClass(ActivePropertyView.class); criteria.add(Restrictions.eq("featured",true)); List<ActivePropertyView> result = entityManager.execute( new HibernateCallback<List<ActivePropertyView>>() { @Override public List<ActivePropertyView> doInHibernate(Session session) throws HibernateException,sqlException { return criteria.getExecutableCriteria(session).setMaxResults(5).list(); } }); return result;
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码