Hibernate : Finding Object

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


Hibernate 5.3 features Hibernate 5 JPA Configuration Hibernate 5 persistence.xml example Hibernate 5 JPA Tutorial Hibernate and JPA Example How properties of a class are mapped to the columns of a database table in Hibernate? Hibernate 5 Annotations Maven dependency Hibernate 5 annotationconfiguration Doctype Hibernate-configuration 5 Hibernate 5 Dialect Hibernate 5 configuration dtd Hibernate 5 Hello World Getting started with Hibernate 5 Hibernate 5.2.10 jar Download Hibernate 5.2.10 Maven Dependency Hibernate 5 Maven Dependency 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 : 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("\t\t" + employee.getSalary());
System.out.print("\t" + employee.getDateOfJoin());

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

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



Discuss: Hibernate : Finding Object  

Post your Comment

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