April 28 2015

Named query in hibernate

Named query in hibernate

A named query is query which can be defined once and use multiple times. Named queries are useful if you want to reuse complex or same query at various places. Named queries stored in hibernate mapping files are easier to maintain than queries scattered all over the Java code. It has a name to be identified and the name have to be unique.

Hibernate named queries are easy to cache if necessary and provides a slight performance advantage. The getNamedQuery() method obtains a Query instance for a named query.


In the below example, findStudentById and findStudentByName are the names defined for the named queries. You also have the parameters in the named query.

Annotation – Student.java

If you are using annotations for mapping, named queries can be created using @NamedQueries and @NamedQuery annotations.

package com.answersz.dao;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import org.hibernate.annotations.NamedQueries;
import org.hibernate.annotations.NamedQuery;

@NamedQuery(name = "findStudentById", query = "from Student e where e.studentId = :studentId"),
@NamedQuery(name = "findStudentByName", query = "from Student e where e.studentName = :studentName") })
@Table(name = "student")
public class Student {

@Column(name = "STUDENT_ID")
private Integer studentId;

@Column(name = "STUDENT_NAME")
private String studentName;

@Column(name = "FATHER_NAME")
private String fatherName;

@Column(name = "DATE_OF_BIRTH")
private Date dateofBirth;

@Column(name = "COURSE")
private String course;

// getters and setters


Xml –  Student.hbm.xml

If you are using xml for mapping, named queries can be created using query tag.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<class name="com.answersz.dao.Student" table="student">

<id name="studentId" type="int" column="STUDENT_ID">
<generator class="native" />
<property name="studentName" type="string" not-null="true"
length="100" column="STUDENT_NAME" />

<property name="fatherName" type="string" not-null="true"
length="100" column="FATHER_NAME" />

<property name="dateofBirth" type="date" not-null="true"
column="DATE_OF_BIRTH" />

<property name="course" type="string" not-null="true" length="100"
column="COURSE" />


<query name="findStudentById">
<![CDATA[from Student e where e.studentId = :studentId]]>

<query name="findStudentByName">
<![CDATA[from Student e where e.studentName = :studentName]]>

Test Class: NamedQueryExample.java

package com.answersz.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

// Named query in hibernate
public class NamedQueryExample {

public static void main(String[] args) {

SessionFactory factory = new AnnotationConfiguration().configure()

Session session = factory.openSession();

System.out.println(" Named query in hibernate ");

Query query = session.getNamedQuery("findStudentById");
query.setParameter("studentId", new Integer(666));

List<Student> students = query.list();

for (Student s : students) {
System.out.println(" Name : " + s.getStudentName());
System.out.println(" Father Name : " + s.getFatherName());
System.out.println(" Course : " + s.getCourse());





 Named query in hibernate
Hibernate: select student0_.STUDENT_ID as STUDENT1_0_, student0_.COURSE as COURSE0_, student0_.DATE_OF_BIRTH as DATE3_0_, student0_.FATHER_NAME as FATHER4_0_, student0_.STUDENT_NAME as STUDENT5_0_ from student student0_ where student0_.STUDENT_ID=?
 Name : MARK
 Father Name : Anderson
 Course : MS

Copyright © 2014. All rights reserved.

Posted April 28, 2015 by answersz in category Hibernate

1 thoughts on “Named query in hibernate

  1. Ramesh M

    The named queries are compiled when the SessionFactory started.So the obvious advantage here is if your query is not proper it fails in the server start up only rather than during the run time.


Leave a Comment

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