Hibernate : Finding Object

In this section we will discuss about session.find() method.

Tutorials   
Hibernate 5 native query example Hibernate 5 query deprecated When should you use Hibernate? What's new in Hibernate 5? How much time it takes to learn Hibernate? Hibernate envers custom revinfo table Hibernate 5 Envers Hibernate Envers get all revisions Hibernate Examples Hibernate 5 build SessionFactory Example Hibernate 5 SessionFactory Example Hibernate 5 Annotation Example How to download Hibernate 5.x? Hibernate 5 Features Hibernate 5 Tutorials Learning Hibernate ORM - Free Training/Tutorial videos of Hibernate ORM with Java How to write hello world example program in Hibernate? How to create one to one relationship in Hibernate 4 with Annotation? How to calculate minimum in Hibernate using the min() Function Hibernate ORM 4.2.21.Final Released - Create CURD application in latest Hibernate Hibernate ORM 4.2.21 Tutorial - Learn Hibernate ORM Framework Hibernate Error: Failed to create sessionFactory object.java.lang.NoClassDefFoundError How to create Hibernate Project in Eclipse? Examples of Hibernate Criteria Query Hibernate ORM 4.3.6.Final released Hibernate ORM 4.2.14.Final released Hibernate 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate association and join example Learn Hibernate programming with Examples Introduction to Hibernate Framework Hibernate Tutorial: Learn Hibernate with examples Hibernate 4.3 Hello World: Example Hibernate ORM 4.3.1.Final released How to create SessionFactory in Hibernate 4.3.1? Hibernate 4 Hello World: Example of Hello World program Hibernate 4 create Session Factory: Example of creating Session Factory in Hibernate 4 Hibernate 4.3 and JPA 2.1 Example What is Hibernate in Java? Hibernate Overview and Architecture What is JPA 2.1?

Ads

Ads

Hibernate : Finding Object

In this section we will discuss about session.find() method.

Hibernate session.find():

find() method is used for finding the object into the database. This method  is deprecated.
The signatures for the various find() methods are -

public List find(String query);
public List find(String query,Object value, Type type);
public List find(String query,Object[] values, Type[] types);

The basic find() method accepts a string .
you pass query string to the method based on some criteria that need to be satisfied in order to return a particular object from the database.
find() method returns List because there might be more than one object.

Example :In this example we are using find() method to list such employee whose empId is greater than 4 .

package net.roseindia.main;

import java.util.*;
import net.roseindia.table.Employee;
import net.roseindia.util.HibernateUtil;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;

public class MainClass {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.openSession(); // session is opened
Transaction transaction = session.beginTransaction();
List emp = session.find("FROM Employee WHERE id > 4");
Iterator iterator = emp.iterator();
System.out.println("Employee Name\tSalary\tDate of Join");
while (iterator.hasNext()) {
Employee employee = (Employee) iterator.next();
System.out.print(employee.getName());
System.out.print("\t\t" + employee.getSalary());
System.out.print("\t" + employee.getDateOfJoin());
System.out.println();
}

session.close(); // session is closed
}
}

Output :

Hibernate: select employee0_.emp_id as emp1_0_, employee0_.date_of_join as date2_0_, employee0_.name as name0_, employee0_.salary as salary0_ from employee employee0_ where employee0_.emp_id>4
Employee Name     Salary     Date of Join
Rowdy             20000      2012-06-27 00:00:00.0
Linda             20000      2010-06-12 00:00:00.0

Click here to download complete source code

Advertisements

Share on Google+Share on Google+

Hibernate : Finding Object

Posted on: August 9, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Hibernate : Finding Object  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads