Detachedcriteria in hibernate

Detachedcriteria in hibernate

How to apply detachedcriteria in hibernate for given Query?

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 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 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
HIBERNATE   What is difference between Jdbc and Hibernate
hibernate
hibernate  what is hibernate flow
hibernate
hibernate  what is hibernate listeners
hibernate
hibernate  how to uses optional column in hibernate
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  hibernate delete query execution
hibernate
hibernate  what is hibernate why we use
Hibernate
Hibernate  how to use pagination concept 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
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  pls give one simple example program for hibernate
Hibernate
Hibernate  Please tell me the difference between hibernate and jdbc
hibernate
hibernate  why we should use hibernate projection
Hibernate
Hibernate  hello, how to run hibernate program in netbeans?please send me step by step instructions to run hibernate in netbeans
hibernate
hibernate  can any one one explain what is hibernate ?   Please visit the following link: Hibernate Tutorials
hibernate
hibernate  how to execute a sequence(in database) from Hibernate program or java program   Please visit the following link: Hibernate Tutorials
Hibernate
Hibernate  How do you handle parent - child tables relationships in hibernate? How do you handle Detach state in hibernate
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 - Hibernate
Hibernate Features  Hibernate features in eclipse
hibernate
hibernate  how to impot mysql database client jar file into eclipse for hibernate configuration   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 http://www.roseindia.net/hibernate/runninge-xample.shtml
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 know how many columns changed in a table when we are using hibernate
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 u connects database using hibernate 3.0? Where you have written the database configuration details
hibernate
hibernate   i am using hibernate3.2.5 and oracle9i. First i set classpath to ojdbc14.jar and all the hibernate jar files to path env variable.After that when i try to execute the program i got an error "unknown oracle major
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
hibernate
hibernate  when we using schemaExport in hibernate every time it drops the existing table and create new table ,if table contain dependent table then is it drop
hibernate - Hibernate
hibernate program shareware  hibernate program shareware example
Hibernate - Hibernate
Hibernate sessionfactory configuration  What is the process of hibernate sessionfactory configuration
Hibernate
of Hibernate. I followed all th steps as given in the tutorial, but a build error... hibernate library files are not included in the project build path.ADS_TO_REPLACE_1 Please download the example from Hibernate Getting Started Tutorial. Please
Hibernate
Hibernate  Hi i have 2 doubbts regarding Hibernate ,.. 1)Can we rename hibernate.cfg.xml? 2? can we use multiple mapping resource in hibernate.cf.xml file ? pls let me know soon
Hibernate
Hibernate  Can we write more than one hibernate.cfg.xml file... ? if so how can we call and use it.? can we connect to more than one DataBase from a single Hibernate program
hibernate
://www.roseindia.net/hibernate/index.shtml Here you will get lot of examples. ThanksADS
Hibernate - Hibernate
Hibernate application development help  Hi, Can anyone help me in developing my first Java and Hibernate application
Hibernate - Hibernate
prog using hibernate query   Hibernate update query http://www.roseindia.net/hibernate/hibernate-update.shtml This will show you how to update records in hibernate! regards Meeya
hibernate...............
hibernate...............  goodevining. I am using hibernate on eclipse, while connection to database Orcale10g geting error .........driver ARNING: SQL Error: 0, SQLState: null 31 May, 2012 8:18:01 PM
hibernate - Hibernate
hibernate  is there any tutorial using hibernate and netbeans to do a web application add,update,delete,select   Hi friend, For hibernate tutorial visit to : http://www.roseindia.net/hibernate/index.shtml
hibernate - Hibernate
hibernate  pl send me hibernate interview questions
Hibernate Criteria With Collections
Hibernate Criteria With Collections An example of hibernate criteria...); DetachedCriteria contactCriteria = DetachedCriteria .forClass(Contact.class...()); } } } Here is the video tutorial of "Hibernate Criteria Query
How to use OR operator in hibernate Detached criteria query?
How to use OR operator in hibernate Detached criteria query?  How to use OR operator in hibernate Detached criteria query?   You can use OR operator in hibernate detachedCriteria through Restrictions.disjunction
hibernate - Hibernate
hibernate    what is the lazy Loading in hibernate? can you give the example table ?  Lazy Loading in Hibernate In Hibernate, Lazy... in the program's operation. The syntax of lazy loading in Hibernate... public Entity
hibernate - Hibernate
: http://www.roseindia.net/hibernate/hibernate-aggregate-functions.shtml http://www.roseindia.net/hibernate/associations-and-joins.shtml Thanks
Hibernate - Hibernate
Hibernate  how to call the stored procedure in mysql database server using hibernate..??  Hi Friend, Please visit the following link: http://www.roseindia.net/hibernate Thanks
Hibernate Query - Hibernate
Hibernate Query  Hibernate Query

Ads