Share on Google+Share on Google+

ratna rathor
Detachedcriteria in hibernate
1 Answer(s)      4 years and 10 months ago
Posted in : Hibernate

How to apply detachedcriteria in hibernate for given Query?

Ads

View Answers

June 1, 2012 at 5:37 PM


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.
For it you have to import

org.hibernate.criterion.DetachedCriteria;

Here is a way to write DetchedCriteria -

DetachedCriteria query = DetachedCriteria.forClass(Employee.class);
query.add(Property.forName("name").eq("Ron"));

Next you can obtain criteria by passing any arbitrary session to getExecutableCriteria() as ?

employeeList = query.getExecutableCriteria(session).list();

Here is 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;

public class DetachedCriteriaQuery {
    public static void main(String[] args) {
        DetachedCriteria query = DetachedCriteria.forClass(Employee.class);
        query.add(Property.forName("name").eq("Ron"));
        SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        List<Employee> employeeList = new ArrayList<Employee>();
        employeeList = query.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();

    }
}









Related Tutorials/Questions & Answers:
Detachedcriteria in hibernate
Detachedcriteria in hibernate  How to apply detachedcriteria in hibernate for given Query?   Detached Criteria query is a facility provide by Hibernate to write criteria queries in ?detached mode?, where hibernate
Hibernate DetachedCriteria mutiple results in java
Hibernate DetachedCriteria mutiple results in java  This is the SQL... the equivalent to hibernate DetachedCriteria and expected to only have two data as a result. DetachedCriteria dc = getDetachedCriteria(); DetachedCriteria
Advertisements
Hibernate Criteria Detached Subquery Using "Subqueries"
Hibernate Criteria Detached Subquery Using "Subqueries" In this tutorial you will learn how to express a subquery using DetachedCriteria Subqueries. In Hibernate Criteria with the help of DetachedCriteria a query can
Hibernate Criteria Detached Subquery Example
Hibernate Criteria Detached Subquery Example In this tutorial you will learn how to express a subquery using DetachedCriteria. As you have been read in the previous example Hibernate Criteria Detached Query Subquery Hibernate
Hibernate Criteria Nested Properties
Hibernate Criteria Nested Properties Consider the example based on Hibernate Criteria Nested Properties DetachedCriteria detachedCriteria = DetachedCriteria .forClass(Student.class); detachedCriteria.add(Restrictions.eq("name
HIBERNATE
HIBERNATE   What is difference between Jdbc and Hibernate
hibernate
hibernate  what is hibernate flow
hibernate
hibernate  what is hibernate listeners
Hibernate
Hibernate  how to use pagination concept in Hibernate
hibernate
hibernate  what is hibernate why we use
Hibernate
Hibernate  hibernate delete query execution
hibernate
hibernate  please give me the link where i can freely download hibernate software(with dependencies)   Learn hibernate, if you want to learn hibernate, please visit the following link: Hibernate Tutorials
hibernate
hibernate  how to uses optional column in hibernate
hibernate
hibernate   I want to learn how to use hibernate framework in web application . for storing database in our application
hibernate
hibernate   I want to learn how to use hibernate framework in web application . for storing database in our application
How to use AND OR operator in hibernate Detached criteria query?
How to use AND OR operator in hibernate Detached criteria query?  How to use AND OR operator in hibernate Detached criteria query?   You can use AND, OR operator in hibernate detachedCriteria through
hibernate
why hibernate?  why hibernate?   Hibernate: -Hibernate... library. It solves object-relational impedance mismatch problems. Hibernate makes the application development process easy Hibernate saves the development
Hibernate
Hibernate  Please tell me the difference between hibernate and jdbc
hibernate
hibernate  why we should use hibernate projection
hibernate
hibernate  pls give one simple example program for hibernate
Hibernate
Hibernate  How do you handle parent - child tables relationships in hibernate? How do you handle Detach state in hibernate
hibernate
hibernate  how to execute a sequence(in database) from Hibernate program or java program   Please visit the following link: Hibernate Tutorials
hibernate
hibernate  can any one one explain what is hibernate ?   Please visit the following link: Hibernate Tutorials
Hibernate
Hibernate  hello, how to run hibernate program in netbeans?please send me step by step instructions to run hibernate in netbeans
Hibernate
Hibernate  hi sir i need hibernate complete tutorial for download   Hi Friend, Please visit the following link: Hibernate Tutorials Thanks
hibernate
hibernate  how to impot mysql database client jar file into eclipse for hibernate configuration   Hi Friend, Please visit the following link: http://www.roseindia.net/hibernate/runninge-xample.shtml Thanks
Hibernate - Hibernate
Hibernate Features  Hibernate features in eclipse
HIBERNATE
HIBERNATE  How u connects database using hibernate 3.0? Where you have written the database configuration details
hibernate
hibernate  hi what are the necessary jar files that we need to set in the classpath to execute a hibernate application
hibernate
hibernate  How to know how many columns changed in a table when we are using hibernate
hibernate
hibernate  Is there any other way to call procedure in hibernate other than named query????? if we are using session object to get the connection then why hibernate we can directly call by using a simple java class??????? please
Tutorials   
Java Spring Hibernate Struts Training 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? Can you provide me Hibernate configuration tutorial? I want to build sessionfactory in hibernate 4. Need help. I want to learn Hibernate tutorial quickly How to create sessionfactory in Hibernate 4.1.1? How to create sessionfactory in Hibernate 4.1? Hibernate 4.3.0.Final get session How to learn hibernate in 24 hours? hibernate 4.3.0 I want to know how can I create sessionfactory in hibernate 4 (latest version) standard serviceregistrybuilder example

Ads

 
Advertisement null

Ads