Share on Google+Share on Google+

Hibernate Named HQL using XML Mapping

In this section, you will learn executing Named HQL written in XML Mapping file.

Hibernate Named HQL using XML Mapping

In this section, you will learn executing Named HQL written in XML Mapping file.

In the below example, we will write HQL query inside <query> tag in XML mapping file and execute it directly from java class file. You can check this mapping below in Worker.hbm.xml file.

We execute the query by getting the query instance using getNamedQuery()  method and passing the conditional where clause value using setString() method. Below, you can check this in the file.

The project hierarchy is given below :



<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>

<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="show_sql">true</property>
<property name="">validate</property> 

package net.roseindia;

import java.util.Date;

public class Worker {

private Long workerId;

private String firstname;

private String lastname;

private Date birthDate;

private String cellphone;

public Worker() {


public Worker(String firstname, String lastname, Date birthdate,
String phone) {
this.firstname = firstname;
this.lastname = lastname;
this.birthDate = birthdate;
this.cellphone = phone;


public Long getWorkerId() {
return workerId;

public void setWorkerId(Long workerId) {
this.workerId = workerId;

public String getFirstname() {
return firstname;

public void setFirstname(String firstname) {
this.firstname = firstname;

public String getLastname() {
return lastname;

public void setLastname(String lastname) {
this.lastname = lastname;

public Date getBirthDate() {
return birthDate;

public void setBirthDate(Date birthDate) {
this.birthDate = birthDate;

public String getCellphone() {
return cellphone;

public void setCellphone(String cellphone) {
this.cellphone = cellphone;


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<hibernate-mapping package="net.roseindia">

<class name="Worker" table="worker">
<id name="workerId" column="worker_id">
<generator class="native" />
<property name="firstname" column="firstname" />
<property name="lastname" column="lastname" />
<property name="birthDate" type="date" column="birth_date" />
<property name="cellphone" column="cell_phone" />

<query name="findViaWorkerIdHQL">from Worker w where w.workerId = :workerId</query>

package net.roseindia;

import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

public class HibernateNamedHQLusingXML {
private static SessionFactory sf;
private static ServiceRegistry serviceRegistry;

public static void main(String[] args) {
try {
Configuration configuration = new Configuration().addResource("net/roseindia/Worker.hbm.xml");
serviceRegistry = new ServiceRegistryBuilder().applySettings(
sf = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
System.err.println("Failed to create sessionFactory object." + ex);
throw new ExceptionInInitializerError(ex);
System.out.println("*** Hibernate Named HQL with XML Mapping ***");
Session session = sf.openSession();
try {
Query query = session.getNamedQuery("findViaWorkerIdHQL").setString("workerId", "13");
List workers = query.list();

for (Iterator iterator = workers.iterator(); iterator.hasNext();) {
Worker worker = (Worker);
System.out.println("Worker Id:" + worker.getWorkerId());
System.out.println("Worker Name:" + worker.getFirstname());
System.out.println("Worker Birth Date:" + worker.getBirthDate());
System.out.println("Worker CellPhone No."+ worker.getCellphone());
} catch (HibernateException e) {
if (session.getTransaction() != null)
} finally {


After execution, you will get the following results :

*** Hibernate Named HQL with XML Mapping ***
Hibernate: select worker0_.worker_id as worker1_0_, worker0_.firstname as firstname0_, worker0_.lastname as lastname0_, worker0_.birth_date as birth4_0_, worker0_.cell_phone as cell5_0_ from worker worker0_ where worker0_.worker_id=?
Worker Id:13
Worker Name:Somesh
Worker Birth Date:1987-07-21
Worker CellPhone No.919595959090

Download Source Code


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

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Hibernate Named HQL using XML Mapping  

Post your Comment

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