Hibernate ReturningWork and Session.doReturningWork Example for JDBC
February 12, 2015
In this page we will learn Hibernate ReturningWork and Session.doReturningWork example for JDBC. From hibernate session java connection object will be created and using JDBC interact to database and return the result. org.hibernate.jdbc.ReturningWork has execute() method that has argument as java Connection. To use in our hibernate program, there is a method in hibernate Session as given below.
<T> T doReturningWork(ReturningWork<T> work)
ReturningWork
org.hibernate.jdbc.ReturningWork interacts through JDBC to fetch data from database and then return it. The JDBC connection instance is provided by hibernate. ReturningWork has a method as below.T execute(Connection connection): This method is implemented to interact with database using JDBC. The Connection argument is provided by hibernate. Using this connection, we can fetch java PreparedStatement, CallableStatement and Statement. Finally return the data.
Session.doReturningWork
org.hibernate.Session.doReturningWork is the method which accepts argument of hibernate ReturningWork. After completing the work, the object retuned by ReturningWork is finally returned to calling doReturningWork.Complete Example
Find the complete example for the demo of Hibernate 4 ReturningWork and Session.doReturningWork.Input Data
Find the print screen of database table which we are using as an input for our demo.Main Class
Find the main class to access data.ReturningWorkDemo.java
package com.concretepage; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.hibernate.Session; import org.hibernate.jdbc.ReturningWork; public class ReturningWorkDemo { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); session.beginTransaction(); List<Student> students = session.doReturningWork( new ReturningWork<List<Student>>() { @Override public List<Student> execute(Connection connection) throws SQLException { PreparedStatement preparedStatement= connection.prepareStatement("select id, age, name from student where age =?"); preparedStatement.setInt(1, 23); ResultSet rs = preparedStatement.executeQuery(); List<Student> list = new ArrayList<Student>(); while (rs.next()) { list.add(new Student(rs.getInt(1), rs.getInt(2), rs.getString(3))); } return list; } }); for(Student s: students){ System.out.println("Id:"+s.getId()+", Age:"+s.getAge()+", Name:"+s.getName()); } session.close(); } }
Bean
Find the bean being used in the example.Student.java
package com.concretepage; public class Student { private int id; private int age; private String name; public Student(int id, int age, String name){ this.id = id; this.age = age; this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
Output
Find the output.Id:1, Age:23, Name:Rahim Id:2, Age:23, Name:Mahesh