HQL (Hibernate Query Language) in Hibernate

Home / HQL (Hibernate Query Language) in Hibernate

For CRUD operation in hibernate we use HQL(Hibernate Query Language)

HQL is same as SQL the only difference is that in HQL we use Entity Class name instead of Table Name

Example:

in Sql we use query for select data like
select * from tableName

in HQL we use query for select data like
from className

Hibernate provide Query Interface Which extends BasicQueryContract and present inside org.hibernate package.

Syntax For Query:
Query query = session.createQuery(“Write Query Here”);

Here are some example of crud operation using hibernate

1- Insert data using HQL

Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
Transaction t = session.beginTransaction();

HelloWord hw = new HelloWord();
hw.setName(“Vashu”);
hw.setLastName(“Rajput”);
session.save(hw);

t.commit();
session.close();
System.out.println(“Succesfully Done”);

2- Select data using HQL

Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
Transaction t = session.beginTransaction();

org.hibernate.Query q = session.createQuery(“from HelloWord”);
List<HelloWord> list = q.list();
for (HelloWord h : list) {
System.out.println(“Name= ” + h.getName() + ” && lastName= ” + h.getLastName());
}

t.commit();
session.close();
System.out.println(“Succesfully Done”);

3- Select data with where clouse using HQL

Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
Transaction t = session.beginTransaction();

org.hibernate.Query q = session.createQuery(“from HelloWord where id = :id”);
q.setParameter(“id”, 1);
List<HelloWord> list = q.list();
for (HelloWord h : list) {
System.out.println(“Name= ” + h.getName() + ” && 
lastName= ” + h.getLastName());
}

t.commit();
session.close();
System.out.println(“Succesfully Done”);

4- Update table data Using HQL

Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
Transaction t = session.beginTransaction();

String insertQuery = “Update HelloWord set name = :name 
where id= :id”;
org.hibernate.Query query = 
session.createQuery(insertQuery);
query.setParameter(“name”, “JavaDream”);
query.setParameter(“id”, 2);
int updateStatus = query.executeUpdate();
if (updateStatus > 0) {
System.out.println(“Succesfully Update Table Data”);
}

t.commit();
session.close();
System.out.println(“Succesfully Done”);

5- delete table data using HQL

Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
Transaction t = session.beginTransaction();

String deleteQuery = “delete from HelloWord where id = :id”;
org.hibernate.Query q = session.createQuery(deleteQuery);
q.setParameter(“id”, 1);
int deleteStatus = q.executeUpdate();
if (deleteStatus > 0) {
System.out.println(“Succesfully Deleted”);
}

t.commit();
session.close();
System.out.println(“Succesfully Done”);

Now Follow Steps To create a CRUD operation programe using Hibernate

1- Create a Maven Project (File -> New Project -> maven -> JavaApplication)

2- Add  Some Dependencies inside pom.xml file

3- Add hibernate.cfg.xml file inside src\main\resources folder

4- Create Entity Class

5- Create MainConfiguration Class

Now do some programming and follow the above steps:

Project Structure

1- Create a maven project

2- add following dependencies in your pom.xml file inside <dependencies></dependencies> tag

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.5.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.5.Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.0-Final</version>
</dependency>

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>

<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>

3- create hibernate.cfg.xml file inside resources folder and add the following configuration

<?xml version=”1.0″ encoding=”utf-8″?>
<!DOCTYPE hibernate-configuration PUBLIC
“-//Hibernate/Hibernate Configuration DTD 3.0//EN”
http://hibernate.sourceforge.net/hibernate-configuration-3.…“>
<hibernate-configuration>
<session-factory>
<property name=”hibernate.bytecode.use_reflection_optimizer”>false</property>
<property name=”hibernate.connection.driver_class”>com.mysql.jdbc.Driver</property>
<property name=”hibernate.connection.password”>12345@</property>
<property name=”hibernate.connection.url”>jdbc:mysql://localhost:3306/HibernateTutorial</property>
<property name=”hbm2ddl.auto”>update</property>
<property name=”hibernate.connection.username”>root</property>
<property name=”hibernate.dialect”>org.hibernate.dialect.MySQLDialect</property>
<property name=”show_sql”>true</property>
<mapping class=”com.vasu.hibernatehellowordexample.HelloWord”></mapping>
</session-factory>
</hibernate-configuration>

4- create Entity class in your main package and write the following code

package com.vasu.hibernatehellowordexample;

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

/**
*
* @author Vasu Rajput
*/
@Entity
@Table(name = “HelloWordTable”)
public class HelloWord {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = “Name”)
private String name;
@Column(name = “LastName”)
private String lastName;

public HelloWord() {
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

}

5- Create a MainConfiguration Class For Hibernate and write the following code inside that

package com.vasu.hibernatehellowordexample;

import java.util.List;
import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

/**
*
* @author Vasu Rajput
*/
public class MainConfig {

public static void main(String[] args) {
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
Transaction t = session.beginTransaction();

// For Insert Data
HelloWord hw = new HelloWord();
hw.setName(“Vishu”);
hw.setLastName(“Rajput”);
session.save(hw);

// For Select Data
org.hibernate.Query q = session.createQuery(“from HelloWord”);
List<HelloWord> list = q.list();
for (HelloWord h : list) {
System.out.println(“Name= ” + h.getName() + ” && lastName= ” + h.getLastName());
}

// Select Using Where Clouse
org.hibernate.Query q1 = session.createQuery(“from HelloWord where id = :id”);
q1.setParameter(“id”, 1);
List<HelloWord> list1 = q1.list();
for (HelloWord h : list1) {
System.out.println(“Name= ” + h.getName() + ” && lastName= ” + h.getLastName());
}

//For Update Data
String insertQuery = “Update HelloWord set name = :name where id= :id”;
org.hibernate.Query query = session.createQuery(insertQuery);
query.setParameter(“name”, “JavaDream”);
query.setParameter(“id”, 2);
int updateStatus = query.executeUpdate();
if (updateStatus > 0) {
System.out.println(“Succesfully Update Table Data”);
}

//For Delete Data
String deleteQuery = “delete from HelloWord where id = :id”;
org.hibernate.Query q2 = session.createQuery(deleteQuery);
q2.setParameter(“id”, 1);
int deleteStatus = q2.executeUpdate();
if (deleteStatus > 0) {
System.out.println(“Succesfully Deleted”);
}

t.commit();
session.close();
System.out.println(“Succesfully Done”);

}
}

 

Help Others, Please Share

About Author

Leave a Reply

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

x