HQL Tutorial HQL select, Update And Delete Queries

In this example we are going to see how to use Hibernate Query Language (HQL).


Project Structure :

Create a simple java project in Eclipse, and add hibernate related jar files to the classpath of the project. The final project structure is given below.

One-to-Many Mapping




Create Model Classes :

Student.java

package com.jwt.hibernate;

public class Student {
	private long id;
	private String name;
	private String degree;
	private String roll;
	private String phone;

	public long getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

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

	public String getDegree() {
		return degree;
	}

	public void setDegree(String degree) {
		this.degree = degree;
	}

	public String getRoll() {
		return roll;
	}

	public void setRoll(String roll) {
		this.roll = roll;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

}

Create Hibernate Mapping File :

Create student.hbm.xml file to map the Student class with the database tables.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.jwt.hibernate.Student" table="STUDENT">
		<id column="ID" name="id" type="long">
			<generator class="increment" />
		</id>
		<property column="STUDENT_NAME" name="name" type="string" />
		<property column="DEGREE" name="degree" type="string" />
		<property column="ROLL" name="roll" type="string" />
		<property column="PHONE" name="phone" type="string" />
	</class>
</hibernate-mapping>

Create Hibernate Configuration File:

Create the Hibernate configuration file (hibernate.cfg.xml) to specify database type, connection details and the mapping files:

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory>
	 	<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jwt</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">mukesh</property>
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
		<property name="show_sql">true</property>
		<property name="format_sql">true</property>
		<property name="hbm2ddl.auto">update </property>
		<mapping resource="com/jwt/hibernate/student.hbm.xml" />
	</session-factory>
</hibernate-configuration>

Program to insert data :

Create the Hibernate configuration file (hibernate.cfg.xml) to specify database type, connection details and the mapping files:

InsertData.java

package com.jwt.hibernate;

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

public class InsertData {

	public static void main(String[] args) {

		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml");

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		Student student = new Student();
		student.setName("Ravi");
		student.setRoll("102");
		student.setPhone("8888");
		student.setDegree("B.E");

		Student student1 = new Student();
		student1.setName("Mukesh");
		student1.setRoll("103");
		student1.setPhone("9999");
		student1.setDegree("B.E");

		Transaction tx = session.beginTransaction();
		session.save(student);
		session.save(student1);
		System.out.println("Object saved successfully.....!!");
		tx.commit();
		session.close();
		factory.close();
	}
}


Program to retrieve data

package com.jwt.hibernate;

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 SimpleSelect {

	public static void main(String[] args) {

		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml");

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		Transaction tx = session.beginTransaction();

		// Get All Employees
		Query query = session.createQuery("from Student");
		List<Student> list = (List<Student>) query.list();
		for (Student st : list) {
			System.out.println("List of Employees::" + st.getId() + ","
					+ st.getName() + "," + st.getDegree());
		}
		// Get Employee with id
		query = session.createQuery("from Student where id= :id");
		query.setLong("id", 1);
		Student stu = (Student) query.uniqueResult();
		System.out.println("Student Name=" + stu.getName() + ", Degre="
				+ stu.getDegree());

		tx.commit();
		session.close();
		factory.close();
	}
}

Program to update/delete data

package com.jwt.hibernate;

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 UpdateData {

	public static void main(String[] args) {

		Configuration cfg = new Configuration();
		cfg.configure("hibernate.cfg.xml");

		SessionFactory factory = cfg.buildSessionFactory();
		Session session = factory.openSession();
		Transaction tx = session.beginTransaction();

		// Update Employee
		Query query = session
				.createQuery("update Student set name= :name where id= :id");
		query.setParameter("name", "Amit Raj");
		query.setLong("id", 1);
		int result = query.executeUpdate();
		System.out.println("Student data Update Status=" + result);

		// Delete Employee, we need to take care of foreign key constraints too
		query = session.createQuery("delete from Student where id= :id");
		query.setLong("id", 3);
		result = query.executeUpdate();
		System.out.println("Student Data Delete Status=" + result);

		tx.commit();
		session.close();
		factory.close();
	}
}




comments powered by Disqus