Hibernate Native SQL Query Example
Hibernate Native SQL Query Example
Welcome to the Hibernate Native SQL Query example tutorial. We looked into Hibernate Query Language
and Hibernate Criteria in earlier articles, today we will look into Hibernate Native SQL query with examples.
ad ✕
Slack
It's teamwork, but simpler, more pleasant
and more productive.
https://www.journaldev.com/3422/hibernate-native-sql-query-example 1/12
09/08/2018 Hibernate Native SQL Query Example - JournalDev
Hibernate provide option to execute native SQL queries through the use of SQLQuery object. Hibernate SQL
Query is very handy when we have to execute database vendor speci c queries that are not supported by
Hibernate API. For example query hints or the CONNECT keyword in Oracle Database.
For normal scenarios, Hibernate SQL query is not the recommended approach because we loose bene ts
related to hibernate association and hibernate rst level cache.
I will use MySQL database and same tables and data setup as used in HQL example, so you should check
out that rst to understand the tables and corresponding model classes mapping.
// Prep work
SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
Session session = sessionFactory.getCurrentSession();
When we execute above code for the data setup we have, it produces following output.
https://www.journaldev.com/3422/hibernate-native-sql-query-example 2/12
09/08/2018 Hibernate Native SQL Query Example - JournalDev
Notice that list() method returns the List of Object array, we need to explicitly parse them to double, long
etc. Our Employee and Address classes have following toString() method implementations.
@Override
public String toString() {
return "Id= " + id + ", Name= " + name + ", Salary= " + salary
+ ", {Address= " + address + "}";
}
@Override
public String toString() {
return "AddressLine1= " + addressLine1 + ", City=" + city
+ ", Zipcode=" + zipcode;
}
Notice that our query is not returning Address data, whereas if we use HQL query "from Employee", it
returns the associated table data too.
https://www.journaldev.com/3422/hibernate-native-sql-query-example 3/12
09/08/2018 Hibernate Native SQL Query Example - JournalDev
The output generated will be same, however we will see slight performance improvement when the data is
huge.
{[aliasname].*} is used to return all properties of an entity. When we use addEntity() and addJoin()
with join queries like above it returns both the objects, as shown above.
Id= 3, Name= Lisa, Salary= 300.0, {Address= AddressLine1= BTM 1st Stage,
City=Bangalore, Zipcode=560100}::AddressLine1= BTM 1st Stage, City=Bangalore,
Zipcode=560100::
You can run both the queries in the mysql client and notice that the output produced is same.
query = session
.createSQLQuery("select emp_id, emp_name, emp_salary from
https://www.journaldev.com/3422/hibernate-native-sql-query-example 6/12
09/08/2018 Hibernate Native SQL Query Example - JournalDev
query = session
.createSQLQuery("select emp_id, emp_name, emp_salary from
Employee where emp_id = :id");
empData = query.setLong("id", 2L).list();
for (Object[] row : empData) {
Employee emp = new Employee();
emp.setId(Long.parseLong(row[0].toString()));
emp.setName(row[1].toString());
emp.setSalary(Double.parseDouble(row[2].toString()));
System.out.println(emp);
That’s all for a brief introduction of Hibernate SQL Query, you should avoid using it unless you want to
execute any database speci c queries.
https://www.journaldev.com/3422/hibernate-native-sql-query-example 7/12
09/08/2018 Hibernate Native SQL Query Example - JournalDev
« PREVIOUS NEXT »
Hibernate Log4j Logging Hibernate Named Query Example – @NamedQuery
About Pankaj
If you have come this far, it means that you liked what you are reading. Why not reach little
more and connect with me directly on Google Plus, Facebook or Twitter. I would love to hear
your thoughts and opinions on my articles directly.
Recently I started creating video tutorials too, so do check out my videos on Youtube.
Comments
elon says
DECEMBER 26, 2017 AT 1:01 AM
Thanks u.
Reply
https://www.journaldev.com/3422/hibernate-native-sql-query-example 8/12
09/08/2018 Hibernate Native SQL Query Example - JournalDev
sarath says
MAY 19, 2016 AT 5:56 AM
Hi pankaj,
im sarath,nic explnation and good examples,and i have a problem with my coding when i use
createSQLquery in session factory for executing a native query. it throws an exception like no dialect
mapping for jdbc type 1111.
im using springs and hibernate and my database is db2, pls giv me a solution for this.
Reply
Anky says
JULY 15, 2016 AT 2:23 AM
You can set dialect for the particular database in hibernate con guration that may solve your issue.
Reply
Maurice says
APRIL 22, 2016 AT 6:59 AM
You saved my day! Thanks for the clear examples!
Reply
Kiran says
DECEMBER 15, 2015 AT 9:39 PM
How we use subquery like- select name ,(select state_name from Mst_State where stateid=1 ) from
Mst_emp where emp_code =1000; data show in jsp
Reply
sathish says
NOVEMBER 23, 2015 AT 11:37 PM
Hello All,
How can I call the stored procedure which is having out perimeter from hibernate native sql queries?
Thanks in advance.
Reply
Supraja says
MARCH 10, 2015 AT 5:28 AM
cant we use iterate() to retrieve records from databases while using Native SQL Query??
Reply
https://www.journaldev.com/3422/hibernate-native-sql-query-example 9/12
09/08/2018 Hibernate Native SQL Query Example - JournalDev
Karthikeyan says
DECEMBER 8, 2014 AT 4:51 PM
Thanks for Shareing , it really helps me to understand better
Reply
Leave a Reply
Your email address will not be published. Required elds are marked *
Comment
Name *
Email *
POST COMMENT
https://www.journaldev.com/3422/hibernate-native-sql-query-example 10/12
09/08/2018 Hibernate Native SQL Query Example - JournalDev
HIBERNATE FRAMEWORK
Hibernate Tutorial
› Hibernate Example
› Hibernate SessionFactory
› Hibernate Session get load
› Hibernate Session save
› HQL Example
› Hibernate Criteria
› Hibernate SQL
› Hibernate Named Query
› Hibernate Log4J
› Hibernate Validator
› Hibernate Tomcat DataSource
Hibernate Mapping
› Hibernate One to One Mapping
› Hibernate One to Many Mapping
› Hibernate Many to Many Join Tables
Hibernate Caching
› Hibernate Cache
› Hibernate EHCache
Hibernate Integrations
› Hibernate Spring
› Hibernate Spring MVC
› Hibernate Struts 2
› Hibernate Primefaces
› Hibernate Primefaces Spring
› Hibernate SpringRoo Primefaces
› Hibernate JSF Spring
Miscellaneous
› Hibernate Tools Eclipse Plugin
› Hibernate Con guration O ine
› [Solved] No identi er speci ed
› Hibernate Program Not Terminating
› Access to DialectResolutionInfo
› get is not valid
› No CurrentSessionContext con gured
› Hibernate Interview Questions
https://www.journaldev.com/3422/hibernate-native-sql-query-example 11/12
09/08/2018 Hibernate Native SQL Query Example - JournalDev
© 2018 · Privacy Policy · Don't copy, it's Bad Karma · Powered by WordPress
https://www.journaldev.com/3422/hibernate-native-sql-query-example 12/12