How to use AND operator in hibernate Detached criteria query?

How to use AND operator in hibernate Detached criteria query?

How to use AND operator in hibernate Detached criteria query?

View Answers

June 1, 2012 at 5:43 PM

You can use AND operator in hibernate detachedCriteria through Restrictions.conjunction(). Detached Criteria query is a facility provide by Hibernate to write criteria queries in ?detached mode?, where hibernate session is not available. You can instantiate this class anywhere.

Example :

package net.roseindia.main;

import java.util.*;
import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;

public class DetachedCriteriaAND {
    public static void main(String[] args) {
        DetachedCriteria detachedCriteria = DetachedCriteria
                .forClass(Employee.class);
        detachedCriteria.add(Restrictions.conjunction()).add(
                Property.forName("id").eq(4)).add(
                Property.forName("name").like("R%"));
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        List<Employee> employeeList = new ArrayList<Employee>();
        employeeList = detachedCriteria.getExecutableCriteria(session).list();
        Iterator it = employeeList.iterator();
        while (it.hasNext()) {
            Employee employee = (Employee) it.next();
            System.out.println("Name : " + employee.getName());
            System.out.println("Salary : " + employee.getSalary());
            System.out.println("Date of Join : " + employee.getDateOfJoin());
        }
        session.close();

    }
}

Output :

 Hibernate: select this_.emp_id as emp1_0_0_, this_.date_of_join as date2_0_0_, this_.name as name0_0_, this_.salary as salary0_0_ from employee this_ where 1=1 and this_.emp_id=? and this_.name like ?
Name : Roxi
Salary : 220000
Date of Join : 2001-02-03 00:00:00.0

Description: In this example we are checking two conditions (id must be equal to 4 and name must start with R character) combinedly. Restrictions.conjunction() is used here for AND operator. Restrictions.conjunction() combinedly check both conditions and return true if both conditions are true

Ads









Related Tutorials/Questions & Answers:
How to use AND operator in hibernate Detached criteria query?
How to use AND OR operator in hibernate Detached criteria query?
Advertisements
How to use OR operator in hibernate Detached criteria query?
JPA CriteriaBuilder - How to use ?IN? comparison operator
why we use ^ operator in sql, and what is the meaning of that operator?
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.8.0 is released. Learn to use quarkus-operator-sdk-deployment version 1.8.0 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.8.2 is released. Learn to use quarkus-operator-sdk-deployment version 1.8.2 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.8.4 is released. Learn to use quarkus-operator-sdk-deployment version 1.8.4 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.9.0 is released. Learn to use quarkus-operator-sdk-deployment version 1.9.0 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.9.2 is released. Learn to use quarkus-operator-sdk-deployment version 1.9.2 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.9.3 is released. Learn to use quarkus-operator-sdk-deployment version 1.9.3 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 2.0.0.Beta1 is released. Learn to use quarkus-operator-sdk-deployment version 2.0.0.Beta1 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 2.0.0.Beta2 is released. Learn to use quarkus-operator-sdk-deployment version 2.0.0.Beta2 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 2.0.0.Beta5 is released. Learn to use quarkus-operator-sdk-deployment version 2.0.0.Beta5 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 2.0.0.CR1 is released. Learn to use quarkus-operator-sdk-deployment version 2.0.0.CR1 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.9.5 is released. Learn to use quarkus-operator-sdk-deployment version 1.9.5 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.9.6 is released. Learn to use quarkus-operator-sdk-deployment version 1.9.6 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 2.0.1 is released. Learn to use quarkus-operator-sdk-deployment version 2.0.1 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.8.0.CR1 is released. Learn to use quarkus-operator-sdk-deployment version 1.8.0.CR1 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.8.3 is released. Learn to use quarkus-operator-sdk-deployment version 1.8.3 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.9.1 is released. Learn to use quarkus-operator-sdk-deployment version 1.9.1 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 1.9.4 is released. Learn to use quarkus-operator-sdk-deployment version 1.9.4 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 2.0.0.Beta4 is released. Learn to use quarkus-operator-sdk-deployment version 2.0.0.Beta4 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 2.0.0.CR2 is released. Learn to use quarkus-operator-sdk-deployment version 2.0.0.CR2 in Maven based Java projects
Maven dependency for io.quarkiverse.operatorsdk - quarkus-operator-sdk-deployment version 2.0.0 is released. Learn to use quarkus-operator-sdk-deployment version 2.0.0 in Maven based Java projects
MySQL Arithmetic
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)
Can u print in turbo c 9 8 7 6 5 4 3 2 1 0 using pre increment operator Only use print ++a in the same printf() command ten times)
Java Bitwise XOR "^" Operator
MySQL Boolean Operator
Java Bitwise NOT "~" Operator

Ads