Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
Hibernate
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
Hibernate Criteria Query Example
The Criteria interface allows to create and execute object-oriented queries.
 
 

Hibernate Criteria Query Example

                         

The Criteria interface allows to create and execute object-oriented queries. It is powerful alternative to the HQL but has own limitations. Criteria Query is used mostly in case of multi criteria search screens, where HQL is not very effective. 

The interface org.hibernate.Criteria is used to create the criterion for the search. The org.hibernate.Criteria interface represents a query against a persistent class. The Session is a factory for Criteria instances. Here is a simple example of Hibernate Criterial Query:


 
package roseindia.tutorial.hibernate;

import org.hibernate.Session;
import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
/**
 @author Deepak Kumar
 
 * http://www.roseindia.net 
Hibernate Criteria Query Example

 *  
 */public class HibernateCriteriaQueryExample {
  public static void main(String[] args) {
    Session session = null;
    try {
      // This step will read 
hibernate.cfg.xml and prepare hibernate for

      // use
      SessionFactory 
sessionFactory = 
new Configuration().configure()
          .buildSessionFactory();
      session = 
sessionFactory.openSession
();
      //Criteria Query Example
      Criteria crit = 
session.createCriteria
(Insurance.class);
      List insurances = 
crit.list
();
      for(Iterator it = 
insurances.iterator
();it.hasNext();){
        Insurance insurance = 
(Insuranceit.next();
        System.out.println("
ID: " 
+ insurance.getLngInsuranceId());
        System.out.println("
Name: " 
+ insurance.getInsuranceName());
        
      }
      session.close();
    catch (Exception e) {
      System.out.println(e.getMessage());
    finally {
    }    
  }
}

The above Criteria Query example selects all the records from the table and displays on the console. In the above code the following code creates a new Criteria instance, for the class Insurance:

Criteria crit = session.createCriteria(Insurance.class);

The code:

List insurances = crit.list();

creates the sql query and execute against database to retrieve the data.

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

15 comments so far (
post your own) View All Comments Latest 10 Comments:

Hi,

Can i have some examples regarding session.load with detailed explanation as soon as possible.

Posted by sandeep on Wednesday, 06.4.08 @ 14:07pm | #62064

Hi, in my query I have a drop down box with 6 products, is it possible to make the database automatically fill in the prices for these products in a field when they are selected from the srop down box? If so, how do I do it?!?!?!

If you can help please email me at denis3533@hotmail.com

Thanx!!

Posted by Denis on Wednesday, 04.30.08 @ 05:19am | #58170

"org.apache.commons.collections.LRUMap"'s signer information does not match signer information

Posted by raj on Wednesday, 03.19.08 @ 10:41am | #53284

Dear all,
h r u.i am facing a problem while working with hibernate.i want access multiple tables via hibernate criteria.
how can i perform this operation.

it wouls be ver apeericiated if u can help me or resolve this problem .
thanks
regards
chetan

Posted by Nchetan on Friday, 07.27.07 @ 11:18am | #22032

i want query for inserting one row in view through hibernate.plz solve this.....

Posted by Gaurav Dhote on Monday, 04.23.07 @ 12:25pm | #14794

how many table we will create for many to many relationship exit class.using or mapping technique

Posted by jitendra on Wednesday, 04.18.07 @ 01:02am | #14456

hi,
Your tutorial is really helpful to me.
Thanks for your precious effort.
i am little new to Hibernate.
i have a question here..

HOW CAN I DO JOINS of table[for select and insert operation] using crteria or native SQL in HIBERNATE??



Posted by danesh on Saturday, 03.24.07 @ 14:08pm | #12598

hi buddy,
i have been involved in creating the JAXB & HIBERNATE classes, while in time to create i faced one problem, "COULD WE MAKE SOME COMPLEX ELEMENTS AS COMMON ELEMENTS?" because we can make the elements being as simple elements as COMMON elements. what i mean to say while i am creating the my own XSD can i make some of the complex type elements as a common elements.

please rectify the problem as soon , currently i am involved in the Developement.. \

expecting ur nice reply as soon!!!!

Posted by jegadeesh chandrabose on Tuesday, 01.30.07 @ 12:06pm | #4682

Hi

I did not get any reply or answer for comments posted on this.

Posted by kiran on Wednesday, 01.24.07 @ 16:21pm | #4252

Use Triggers in ur case, as the delete takes place , trigger can collect the data from the old view n can fire an insert accordingly .Try this

Posted by shoaib on Monday, 01.22.07 @ 12:09pm | #4023

Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.