Hibernate Native SQL Query Introduction

In this section, you will learn Hibernate Native SQL query.


 

Ads

Tutorials   
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 4.3.4 Released: Hibernate 4.3.4.Final Maven dependency Hibernate 4.3 Hello World: Example 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? Hibernate 4.3.0.Final Maven dependency Hibernate 4.3 ORM Features Hibernate 4.3 Tutorial Hibernate Criteria average example Hibernate Criteria load all objects from table Hibernate Criteria Hibernate beginner tutorial Hibernate Session.delete() Example Hibernate save or update method example Hibernate Load Example Hibernate save Example - Rose India Hibernate 4 tutorials Hibernate Example Step by Step in Eclipse Hibernate Configuration files Setup Hibernate Environment Hibernate Architecture Hibernate Overview Hibernate 4.2 Tutorial Hibernate 4 Annotations Tutorial Hibernate 4 AnnotationSessionFactoryBean hibernate versions hibernate jndi hibernate tomcat hibernate metamodel generator hibernate entity Seam Hibernate C3P0 Hibernate hibernate shard hibernate means temporal hibernate jsf hibernate spring j2ee hibernate spring hibernate encrypted password alias_to_entity_map hibernate temporal jsf spring hibernate hibernate by example hibernate repository hibernate sample code
Ads

Hibernate Native SQL Query Introduction

In this section, you will learn Hibernate Native SQL query.

Using HQL or criteria query in Hibernate, you can execute nearly any type of SQL query. Even so some developer complaint about slowness of statement generated by Hibernate and they opt to generate their own SQL statement. These developer generated query is known as Hibernate Native SQL Query.

You can create native SQL query using Session as follows :

String sql = "SELECT .........";
Query query = session.createSQLQuery(sql);

Using EntityManager , you can create native SQL query as follows:

String sql = "SELECT .........";
Query q = entityManager.createNativeQuery(sql);

Hibernate Native SQL query can be broadly categorized into following types :

Hibernate Native Scalar query

This is the most basic SQL query. In this query, we fetch list of scalars or values from one or more database tables. Given below a simple example of Native Scalar query :

String sql = "SELECT enrollemnet,name FROM STUDENT";
Query query = session.createSQLQuery(sql);
query.setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP);
List results = query.list();

Hibernate Native Entity query

In this type of query, we fetch all the columns of selected row / rows. Through addEntity( ) method selected data will map automatically to the passed class to the addEntity( ) method.  Given below example of a simple Entity query:

String sql = "SELECT * FROM STUDENT";
Query query = session.createSQLQuery(sql);
query.addEntity(Student.class);
List results = query.list();

Hibernate Named SQL query

In this type of query, we fetch all the columns of selected rows but the WHERE conditional values are passed through named variables as follows :

String sql = "SELECT * FROM STUDENT WHERE id = :enrollment_no";
Query query = session.createSQLQuery(sql);
query.addEntity(Student.class);
query.setParameter("enrollment_no", 321123);
List results = query.list();

ADVANTAGES OF NATIVE SQL

  • We can explicitly use the table specific keywords to fetch the records from the table.

  • Migration from JDBC to hibernate is very simple.

DISADVANTAGES OF NATIVE SQL

The main disadvantage of native SQL is - it makes the application database dependent.

Advertisements

Ads
Share on Google+Share on Google+

Hibernate Native SQL Query Introduction

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

Advertisements

Ads
 

Discuss: Hibernate Native SQL Query Introduction  

Post your Comment


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

Ads

 

Ads