when to use detached criteria? and what is the advantage we get by using detached criterias instead of normal criteria?
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Some.class);
DetachedCriteria criteria = DetachedCriteria.forClass(Some.class);
Thanks!
The Hibernate Criteria Query Language (HCQL) is used to fetch the records based on the specific criteria. The Criteria interface provides methods to apply criteria such as retreiving all the records of table whose salary is greater than 50000 etc.
In Hibernate, the Criteria API helps us build criteria query objects dynamically. Criteria is a another technique of data retrieval apart from HQL and native SQL queries. The primary advantage of the Criteria API is that it is intuitively designed to manipulate data without using any hard-coded SQL statements.
Hibernate provides alternate ways of manipulating objects and in turn data available in RDBMS tables. One of the methods is Criteria API, which allows you to build up a criteria query object programmatically where you can apply filtration rules and logical conditions.
Hibernate Conjunction, is used to add multiple condition in SQL query separated by AND clause within brackets. To generate following query using Hibernate Criteria we need to use Conjunction.
As per docs
Some applications need to create criteria queries in "detached mode", where the Hibernate session is not available. This class may be instantiated anywhere, and then a Criteria may be obtained by passing a session to
getExecutableCriteria()
. All methods have the same semantics and behavior as the corresponding methods of the Criteria interface.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With