Hibernate 5 native Query example to get entity data
In this tutorial we are going to make example program in Hibernate 5 which will use Hibernate 5 native query to get data from database. In this example MySQL is the backend database server. We have used Eclipse IDE for writing and running this example code. I am assuming that you already have MySQL Server installed and running.
Native Query
The word Native Query is used for the SQL query which we can directly run against database through any database client or JDBC query. For example we have a table called employee then the native query (SQL) for getting all the data from database will be:
select * from employee;
This query you can run against database. Hibernate also provides mechanism to run SQL (Native Query) and get data in a Java program. You can run select as well as non-select sql queries using Hibernate Native Query.
Let's get started and write Hibernate 5 Native query example.
Writing Hibernate 5 native query example
First of setup database and project as described in the tutorial Hibernate 5 Annotation Example. After that we will insert some of the data using the following code:
package net.roseindia; import org.hibernate.Session; import org.hibernate.SessionFactory; import net.roseindia.model.*; /** * @author Deepak Kumar * Web: http://www.roseindia.net */ public class CreateData { public static void main(String[] args) throws Exception { SessionFactory sessFact = HibernateUtil.getSessionFactory(); Session session = sessFact.getCurrentSession(); org.hibernate.Transaction tr = session.beginTransaction(); Employee emp1 = new Employee(); emp1.setEmpName("Deepak Kumar"); emp1.setEmpMobileNos("000001"); emp1.setEmpAddress("Delhi - India"); session.save(emp1); Employee emp2 = new Employee(); emp2.setEmpName("Ujjawal Roy"); emp2.setEmpMobileNos("000002"); emp2.setEmpAddress("Delhi - India"); session.save(emp2); tr.commit(); System.out.println("Successfully inserted"); sessFact.close(); } }
If you run above code in Eclipse it will add two records in employee table.
Creating Native Query
In Hibernate 5 we will use the createNativeQuery() method of Session object which returns the Query object. Here is the syntax:
createNativeQuery(java.lang.String sqlString, java.lang.Class resultClass) - This method is used to create an instance of Query passing String SQL query and the Entity class. Finally Query object is used for running a native SQL query.
Here is the full code example:
package net.roseindia; import java.util.Iterator; import java.util.List; import org.hibernate.*; import org.hibernate.query.NativeQuery; import javax.persistence.criteria.CriteriaQuery; import net.roseindia.model.*; /** * @author Deepak Kumar Web: http://www.roseindia.net */ public class GetAllData { public static void main(String[] args) throws Exception { SessionFactory sessFact = HibernateUtil.getSessionFactory(); Session session = sessFact.getCurrentSession(); org.hibernate.Transaction tr = session.beginTransaction(); Listemp = session.createNativeQuery( "SELECT * FROM Employee",Employee.class ) .getResultList(); for (Employee employee : emp) { System.out.println("ID: " + employee.getId()); System.out.println("Name: " + employee.getEmpName()); } tr.commit(); System.out.println("Data printed"); sessFact.close(); } }
The code for constructing and executing native query is:
List emp = session.createNativeQuery( "SELECT * FROM Employee",Employee.class ) .getResultList();
So this way you can run native query in Hibernate 5.
Download Hibernate 5 Native query example code.
Check more Hibernate 5 Tutorials.