• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to use named parameters and named query in Hibernate?

July 26, 2008 //  by Krishna Srinivasan//  Leave a Comment

Named Parameters in Hibernate Query

There is two types of query parameters binding in the Hibernate Query. One is positioned parameter and another one is named parameter. But, hibernate recommend to use the named parameters since it is more flexible and powerful compare to the positioned parameter. Here we will look into the named parameter type in detail.

also read:

  • Introduction to Hibernate
  • Hibernate Interview Questions
  • Interceptors in Hibernate
  • Hibernate Books

Named parameters are as name itself suggests, the query string will be using the parameters in the variable name. That can be replaced at runtime and one advantage of using named parameter is, the same named parameter can be used many times in the same query.
[code lang=”java”] String queryStr = "from Student s where s.name like :searchName";
List result = session.createQuery(queryStr)
.setString("searchName",searchNameValue)
.list;
[/code]

In the above code “:searchName” is the named parameter and it is dynamically added to the query string.

Another good feature is using the named query instead of writing the SQL queries every where. In the named query,all the queries are written inside the .hbm files. Each query is associated with a unique name. Application can load the query by using the name of that query. It avoids writing queries inside the java code itself. The method getNamedQuery() is used for retrieving the query from the mapping file.
[code lang=”java”] session.getNamedQuery("findStudentByName")
.setString("searchName",searchNameValue)
.list();
[/code] [code lang=”html”] <query name="findStudenetbyName"><![CDATA[
from Student s where s.name like :searchName
]]></query>
[/code]

Category: HibernateTag: hibernate query

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Pagination in Hibernate Query API
Next Post: NULL and NOT NULL comparison in the Hibernate API »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact