Hibernate / Hibernate interview questions
Detached Criteria
The detached criteria allows you to create the query without Session. Then you can execute the search in an arbitrary session.
Using a DetachedCriteria is exactly the same as a Criteria except you can do the initial creation and setup of your query without having access to the session. When it comes time to run your query, you must convert it to an executable query with getExecutableCriteria(session).
DetachedCriteria query = DetachedCriteria.forClass(Person.class) .add( Property.forName("sex").eq('F') ); Transaction txn = session.beginTransaction(); List results = query.getExecutableCriteria(session).setMaxResults(100).list(); txn.commit(); session.close();
More Related questions...