Question: How to use AND operator in hibernate Detached criteria query?

Ads

 
 
 

Share on Google+Share on Google+

ratna rathor
How to use AND operator in hibernate Detached criteria query?
1 Answer(s)      4 years and 11 months ago
Posted in : Hibernate

How to use AND operator in hibernate Detached criteria query?

Ads
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









Tutorials   
Java Spring Hibernate Struts Training Hibernate maven dependency example What is the difference between JPA and Hibernate? How to turn off hbm2ddl? Hibernate hbm2ddl.auto values Hibernate table not mapped error - solution Hibernate error class is not mapped Hibernate Envers custom revinfo table Hibernate Envers - REVINFO table doesn't exist How to enable second level cache in Hibernate 5? How to enable second level cache in Hibernate 5? java.lang.IndexOutOfBoundsException: Remember that ordinal parameters are 1-based! org.hibernate.validator cannot be resolved error java.lang.ClassNotFoundException: org.hibernate.cache.TimestampsRegion java.lang.ClassNotFoundException: org.hibernate.cache.TimestampsRegion property hibernate.cache.region.factory_class is not given Hibernate example step by step in Eclipse with MySQL How to make Struts Hibernate and Spring Based Login/Registration Application? What is the use of Hibernate Native SQL? What is the use of Hibernate is used? How to integrate Struts and Hibernate in an application? How to create the program in Java for getting the forgotten Password Form? How to write create JPA Many-to-Many Relationship in Java? What is the DAO Layer in any application? What is Spring Struts Hibernate DAO Layer? How to write Example code for CRUD application in hibernate annotation? Where to update the Hibernate tools? Query conversion convert into hql how to convert sql into hql package org.hibernate.util does not exist C3P0 with Hibernate What do you understand by JPA? Hibernate Query Checker Jpa many to many relationship What do you understand by Hibernate in Java? What are criteria Queries in Hibernate? How to use Associations and Joins in Hibernate? Hibernate 4 Example with Eclipse How to learn Hibernate? What is Hibernate Framework? hibernate faq 15/03/14 hibernate 15/03/2014 Hibernate Criteria Wildcard how to add mulitiple projections this is a hibernate question this is a hibernate question this is a hibernate question how to writw connection pool coding Hibernate 4.3 and JPA 2.1 support How to create Hibernate session factory name?

Ads

 
Advertisement null

Ads