VisualBuilder
  Home > Java > Tutorials > Finding by primary key - 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
 Finding by primary key
  << Prev: Managing Associations Next: Hibernate Query Language (HQL) >>

In the previous section,we tested the basic functionality of our hibernate application. With few lines of code,we were able to perform the basic operations like insert,update and delete on the database table,and add few children without writing a single SQL query. In enterprise applications,an efficient search mechanism is highly needed. Hibernate provides a set of different techniques to search a persisted object. Let us see a simple mechanism to find an Object by primary key without using a query.


Add the following method in UserManager class.


public User getUser(long userId)
{
User user = (User)session.get(User.class, new Long(userId));
return user;
}

This is the whole logic of finding a user by primary key. Let's test this code by adding the following method in TestClient.


  public void testFindByPk(UserManager manager)
{
User user = manager.getUser(1);//Find the user with id=1
if(user == null) {
System.out.println("No user found with ID=1");
}else {
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 ph = (PhoneNumber)numbers.next();
System.out.println("\t\tNumber Type:" ph.getNumberType() "\n"
"\t\tPhone Number:" ph.getPhone());
}
}
}

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


Replace the user id 1 by some reasonable user id that exists in the database and there are few phone numbers added against that user id. You will see the following output.


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

  << Prev: Managing Associations Next: Hibernate Query Language (HQL) >>
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