VisualBuilder
  Home > Java > Tutorials > Using Criteria Queries - Hibernate Tutorial
Tell a friend
Link to us
Total Members
      Members: 84661
     
Sitemap Forum Chat
Home
Hibernate Tutorial Home
1 . Introduction to Java Hibernate
2 . Introduction to Hibernate
3 . The Object/Relational Mapping Problem
4 . JDBC
5 . The Hibernate Alternative
6 . Hibernate Architecture and API
7 . Setting Up Hibernate
8 . Setting up Hibernate - Add the Hibernate libraries
9 . Registration Case Study
10 . Creating the Hibernate Configuration
11 . Writing the first Java File
12 . Writing the mapping file
13 . Writing the Business Component
14 . Writing the Test Client
15 . Managing Associations
16 . Finding by primary key
17 . Hibernate Query Language (HQL)
18 . Using native SQL
19 . Using Criteria Queries
20 . Using Ant to run the project
21 . Using Middlegen to generate source
22 . Review and the next steps
 
 
Java Home
Java Members (27650)
Java Member Articles ( 40 )
Java Discussion (275)
Java Q & A ( 174 )
- Java Ask Question
- Java Questions
- Java Unanswered Questions
Java Resources
Java Source Code (1096)
Java Articles (551)
Java Blogs (118)
Java Jobs (797)
Java Components (85)
Java Books (169)
Java Websites (127)
Java News (103)
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.Net Security,Internationalisation And Deployment
ASP.NET Tutorial
C# Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Hibernate Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (451 )
Source Code (3275 )
Articles (359 )
Books (372 )
Components (1596 )
News (892 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise

Java hibernate Tutorial
 Using Criteria Queries
  << Prev: Using native SQL Next: Using Ant to run the project >>

So far we have seen how to use HQL and native SQL to perform certain database operations. We saw that the HQL was much simpler than the native SQL as we had to provide database columns and the mapping attributes along with mapping classes too to get fully translated java objects in native SQL. Hibernate provides much simpler API called Criteria Queries if our intention is only to filter the objects or narrow down the search. In Criteria Queries,we don't need to provide the select or from clause. Instead,we just need to provide the filtering criteria. For example name like '%a',or id in (1,2,3) etc. Like HQL,the Criteria API works on java objects instead on database entities as in case of native SQL.


 Let's write a method in UserManager to filter the users that have user id within a set of passed user id's.


public java.util.List getUserByCriteria(Long[] items)
{
org.hibernate.Criteria criteria = session.createCriteria(User.class);
criteria.add(org.hibernate.criterion.Restrictions.in("userId", items));
return criteria.list();
}

This method returns the users and associated phone numbers that have one of the user id in items passed as an argument. See how a certain filter criteria is entered using Restrictions class. Similarly we can use Restrictions.like,Restrictions.between,Restrictions.isNotNull,Restrictions.isNull and other methods available in Restrictions class.


Let's write the test code to verify that the filter we provided using Criteria works. Add the following method in TestClient.



public void testFindByCriteria(UserManager manager)
{
java.util.Iterator users = manager.getUserByCriteria(new Long[]{new Long(1),new Long(2)}).iterator();
while(users.hasNext())
{
User user = (User)users.next();
System.out.println("User found with ID="+user.getUserId()+"\n" +
"\tName="+user.getLastName()+"\n" +
"\tEmail="+user.getEmail() +
"");
java.util.Iterator numbers = user.getPhoneNumbers().iterator();
while(numbers.hasNext()) {
PhoneNumber phone = (PhoneNumber)numbers.next();
System.out.println("\t\tNumber Type:"+phone.getNumberType()+"\n" +
"\t\tPhone Number:"+phone.getPhone());
}
}
}


Add the call to this method in main. To do this,add the following line in main.
client.testFindByCriteria(manager);


We passed the two user ids to the function. So we should get the two users (if we have in the database with these user id's) with appropriate phone number entries if they exist in the database. Here is what gets displayed under my environment. You should adjust the appropriate user id's to get the results.


User found with ID=1
Name=Elison
Email=john@visualbuilder.com
Number Type:Office
Phone Number:934757
Number Type:Home
Phone Number:934757
User found with ID=2
Name=Elison
Email=john@visualbuilder.com
Number Type:Home
Phone Number:934757
Number Type:Office
Phone Number:934757

  << Prev: Using native SQL Next: Using Ant to run the project >>
Java Hibernate Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
 
ADVERTISEMENT
Partner List
Code Project
ASP Alliance
More
 
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising