1. In hibernate we have the support to do curd operations on a single object at a time or on multiple objects at a time.
2. By calling the methods save() or update() or load() etc.… we can perform curd operations on a single object at a time.
3. The connection returned by DriverManager class is a non reusable connection. The non reusable connections will increase the burden on a database.
4. To perform curd operations on multiple objects at a time, we have the following three approaches or ways in hibernate.
A. Hibernate Query Language(HQL).
B. Criteria API.
C. Native SQL.
Criteria API:
1. It is another technique to perform multi row operations in hibernate.
2. Using this criteria API, it is only possible to perform select operation on a database. We can not perform non select operations.
3. In criteria API, the queries are generated by hibernate to construct the query we need to pass Class object of a pojo class as a parameter to the hibernate.
4. To store the query constructed by hibernate, we need a reference of org.hibernate.Criteria interface.
5. We can obtain a reference of Criteria interface by calling createCriterai() of Session interface.
Example :
Criteria crit = session.createCriteria(Employee.class);
List list = crit.list();
Iterator it = list.iterator();
while(it.hasNext()){
Employee e = (Employee)it.next();
}
1. While loading object from a database some times we want to filter the objects by using some conditions.
2. If we want to add a condition to criteria object we need a criterion reference.
3. Criterion is an interface of org.hibernate.Creterion package.
4. We can get a reference of criterion interface by calling static method of Restrictions class.
5. Restrictions class is also from org,hibernate.criterion package .
6. There is no direct relationship between criterion interface and Restrictions class. But methods of Restrictions class return criterion reference.
Example 1: The following is the code for loading employee object working in deptno=20
Criteria crit = =session.createCriteria(Employee.class");
Criterion c1 = Restrictions.eq("deptNumber",20);
crit.add(c1);
List list = crit.list();
Example 2: The following is the code for loading Employee object whost name contains s whether it is uppercase or lowercase letters.
Criteria crit = =session.createCriteria(Employee.class");
Criterion c1 = Restrictions.ilike("employeeName","%s%");
crit.add(c1);
List list = crit.list();
Example 3: The following is the code for loading employee object, working in deptno=20 and name contains letter s.
Criteria crit = =session.createCriteria(Employee.class");
Criterion c1 = Restrictions.eq("deptNumber",20);
Criterion c2 = Restrictions.ilike("employeeName","%s%");
Criterion c3 = Restrictions.and(c1,c2);
crit.add(c3);
List list = crit.list();
Example 4: The following is the code for loading employee object working in deptno=20 and name contains a letter "s" and salary in between 1000 and 3000.
Criteria crit = =session.createCriteria(Employee.class");
Criterion c1 = Restrictions.eq("deptNumber",20);
Criterion c2 = Restrictions.ilike("employeeName","%s%");
Criterion c3 = Restrictions.between("employeesal",1000,3000);
Criterion c4 = Restrictions.and(c1,c2);
Criterion c5 = Restrictions.and(c3,c4);
crit.add(c5);
List list = crit.list();
Hibernate Examples Project Github Link : https://github.com/SadaLearningHub1/H...