` Hibernate Query Language HQL Select Query With Example | Java2Study.com

Hibernate Query Language HQL Select Query With Example

Hibernate uses powerful query Hibernate Query Language(HQL) is Similar to Structured Query Language(SQL).HQL is Object Oriented Language and it Supports Inheritance,Polymorphism,association.

HQL will use pojo class names instead of table names and pojo class properties names instead of column names.

by using we can do Insert,select,update,delete operations.that means we can do select and No-select Operations by using The HQL.

HQL Select operation

The select clause retrive which objects and properties to return in the query result set. That means If We want to retrive only few properties of objects instead of Complete Objects,then we will Select clause.

Structure of HQL Select Query

Query query = session.createQuery("From Account a ");
        List list = query.list();

Here is HQL Select Query Example

Tools Used In This Article

  • Hibernate 3.6.10
  • Maven 4.0.0
  • JDK 1.7
  • mysql-connector-java 5.1.6

The Project Structure

Maven Dependencies

Add The Following Dependencies to the pom.xml

  • hibernate-core
  • mysql-connector-java
  • javassist
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">






    package com.practice;
 import java.io.Serializable;

public class Account implements Serializable {
 private int accountId;
 private String name;
 private String address;
 public String getAddress() {
  return address;
 public void setAddress(String address) {
  this.address = address;

 public int getAccountId() {
  return accountId;
 public void setAccountId(int accountId) {
  this.accountId = accountId;
 public String getName() {
  return name;
 public void setName(String name) {
  this.name = name;



<!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    <class name="com.practice.Account" table="account">
        <id name="accountId"  column = "accountid">
            <generator class="assigned" />
        <property name="name" column = "name">   </property>
        <property name="address" column = "address">


   <!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
     <!--  Database Properties -->
 <property name ="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name  ="hibernate.connection.url">jdbc:mysql://localhost:3306/balaDB</property>
 <property name = "hibernate.connection.username">root</property>
 <property name = "hibernate.connection.password">root123</property>
          <!-- Hibernate Properties -->
   <property name = "hibernate.hbm2ddl.auto">validate</property>
   <property name = "hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
 <property name = "hibernate.show_sql">true</property>
 <property name="hibernate.current_session_context_class">
 <!--  mapping files -->
 <mapping resource = "account.hbm.xml"/>

create Test class And Test The code


 package com.practice;

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

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Test {

  * @mankali
 public static void main(String[] args) {
      Configuration configuration = new Configuration();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.getCurrentSession();
       Transaction tx = session.beginTransaction();
         Query query = session.createQuery("From Account a ");
        List list = query.list();
        Iterator itr = list.iterator();
         Object obj = itr.next();
         Account account = (Account)obj;
         System.out.println("Account Id :"+account.getAccountId());
         System.out.println("Account Name :"+account.getName());
         System.out.println("Address :"+account.getAddress());
        System.out.println("data retrived successfully using Hql select...");




SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Hibernate: select account0_.accountid as accountid0_, account0_.name as name0_, 
account0_.address as address0_ from account account0_
Account Id :10003
Account Name :mankali
Address :cccc
Account Id :2001
Account Name :abc
Address :bgr
data retrived successfully using Hql select...

In Database I have Table like following