Tell a friend
Link to us
Bookmark Us
Total Members
      Members: 87081
Sitemap Forum Chat
 
Java Home
Java Members (28736)
Java Member Articles ( 41 )
Java Discussion (283)
Java Q & A ( 172 )
- Java Ask Question
- Java Questions
- Java Unanswered Questions
Java Resources
Java Source Code (1096)
Java Articles (552)
Java Blogs (133)
Java Jobs (801)
Java Components (85)
Java Books (169)
Java Websites (127)
Java News (103)

 
Resource Directory
Java Hosting (0)
Source Code (0)

 
GROUP INFO
Members: 28736
Access Type: Anyone can join

 

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
LINQ Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Javascript Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Grails Tutorial
Hibernate Tutorial
Java 1.6 Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Service-Oriented Architecture (SOA) Tutorial For Managers
Spring Tutorial
Struts Tutorial
Tomcat Tutorial

RESOURCES
Q & A (531 )
Source Code (3276 )
Articles (365 )
Books (373 )
Components (1647 )
News (898 )
Websites (1208 )

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

COMMUNITY
Authors
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Feedback
Tell a Friend
Advertise

 

Home > Java > Member Articles > Database >
 

Hibernate Tutorial


  Author : VisualBuilder.com
  Date Published : 05/13/2007
  Accessed : 21276
   visualbuilder
<< Back  Next >> 

In the previous section,we tried to find a persisted user by user id which was primary key in that case. What if we want to search a user by user name,age or email and none of these attributes is a primary key. Hibernate provides a query language similar to the standard SQL to perform operations on the Hibernate objects. The advantage of using HQL instead of standard SQL is that SQL varies as different databases are adopted to implement different solutions,but HQL remain the same as long as you are within the domain of Hibernate; no matter which database you are using. Let us learn to embed HQL in our example to find the users with age greater than 30. Before doing this,make sure that you have some of the users over the age of 30. For example,i issued the following query on my Oracle console to update the age of few users.


update users set age=31 where user_id in(5,8,9,11,13,14);


 Modify this query to include some of the records in your users table.


Let's write the logic to find the users in our business component. Add the following method in UserManager.


public java.util.List getUsersByAge(int minAage) { org.hibernate.Query q = session.createQuery("from User u where u.age >= :minAge"); q.setInteger("minAge", minAage); return q.list(); } 

Note that the query uses "User" which is the object name and not the table name which is "Users". Similarly the "u.age" is an attribute of the User object and not the column name of the table. So whatever the name of the table may be,and whatever the presentation of that table or columns at the database level may be,we will use the same standard syntax while communicating with the hibernate. Also note the parameter "minAge". The parameters in an HQL query are represented with ":"(colon) character and can be bound using the index or the parameter name in org.hibernate.Query.


Let's write a piece of ode to test this functionality. Add the following method in TestClient.


public void testFindByAge(UserManager manager) { java.util.Iterator users = manager.getUsersByAge(30).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.testFindByAge(manager);


Run the program and see that all the users we modified above are displayed with respective phone numbers.


Following points should be kept in mind when working with HQL.




  • Queries are case insensitive,except the Java class and attribute names. Hence SELECT and select are the same,but User and user are not.




  • If the class or package does not find a place in imports,then the objects have to be called with the package name. For example,if com.visualbuilder.hibernate.User is not imported,then the query would be "from com.visualbuilder.hibernate.User" and so on.




For more details on HQL,visit http://www.hibernate.org/hib_docs/v3/reference/en/html/queryhql.html


Pages   << Back  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  Next >>  Next: Part 18: Using native SQL >>

 
   Printer Friendly
   Email to a friend
   Add to my Favourites  Report Bad Submissions  Submit Feedback
  Download PDF version
 
                 Click each image to add
this page to each site.
 
Related Articles of Java
Previous Article    -     Random Articles    -     Next Article
 
 
 
 
 
 
 
 
Welcome Guest Signup
Member's Panel
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!

SEARCH
 



 
 
 
 
 


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

Warning: Unknown(): write failed: Disk quota exceeded (122) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0