How to use DetchedCriteria in Hibernate
This article mainly introduces how to use DetchedCriteria in Hibernate. It is very detailed and has a certain reference value. Friends who are interested must finish it!
Criteria is bound to Session, and its life cycle ends with the end of Session. When querying with Criteria, objects are dynamically created during execution each time, and various query conditions are added. As Session is recycled, Criteria is also recycled.
To be able to reuse Criteria objects, add org.hibernate.criterion.DetchedCriteria to Hibernate 3. You can first create a DetchedCriteria instance, add various query conditions, and then bind to Session when you need to query to get a Criteria object that binds Session, for example:
/ / first create DetchedCriteria object DetachedCriteria detchedCriteria = DetachedCriteria.forClass (User.class); / / add query condition detchedCriteria.add (Restrictions.ge ("age", new Integer (25)); Session session = sessionFactory.openSession (); / / bind Session and return a Criteria instance Criteria criteria = detchedCriteria.getExecutableCriteria (session); List users = criteria.list () The above is all the content of the article "how to use DetchedCriteria in Hibernate". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!