0% found this document useful (0 votes)
152 views10 pages

Hibernate Query Language: by Raghu Sir (Naresh It, Hyd)

This document provides an overview of Hibernate Query Language (HQL) and examples of how to execute HQL queries within a Hibernate application. It explains that HQL allows executing queries across multiple rows independently of the underlying database, and how table and column names in HQL map to class and field names. It also includes examples of saving objects, fetching all data, and fetching single or multiple columns using HQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views10 pages

Hibernate Query Language: by Raghu Sir (Naresh It, Hyd)

This document provides an overview of Hibernate Query Language (HQL) and examples of how to execute HQL queries within a Hibernate application. It explains that HQL allows executing queries across multiple rows independently of the underlying database, and how table and column names in HQL map to class and field names. It also includes examples of saving objects, fetching all data, and fetching single or multiple columns using HQL queries.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

by RAGHU SIR[NARESH IT, HYD]

Hibernate Query Language


Session(I) : DB Operations
save(),update(),delete(),get(),load()..etc

1 row <=> 1 Object


--------------------------------
HQL : Hibernate Query Language
multi-rows operations
Like
select 3 rows
update 5 rows

SQL=> DB Dependent
HQL => Db Independent

SQL
DB:Oracle : select * from tab (number)
MySQL : show table (int)
---------------------------------------
HQL:??
SQL=> table names and column names
SQL
ex: select ename,esal from emptab where eid=?

SQL=> HQL
then tableName => model class Name
columnName=> variableName
----------------------------------
SQL#1 select ename from emptab;
HQL#1
select empName from in.nit.model.Employee (vaild)
** SQL words are case-insensitive
(ex: SELECT,FROM,WHERE,GROUP BY, HAVING..ETC)
** Java className,variableNames are case-sensitive

Page 1 of 10
by RAGHU SIR[NARESH IT, HYD]

SELECT empId From in.nit.model.Employee (valid)


select empId frOM in.nit.model.Employee (valid)
SELECT EMPID From in.nit.model.Employee (invalid)
SELECT empId From in.nit.model.EMPLOYEE (invalid)

SQL:#2 select * from emptab;


HQL:#2 select * from in.nit.Employee;(invaild)
from in.nit.Employee(valid)
select e from in.nit.Employee e(vaild)
---------------------------------------------
Examples:-
1. Define one HQL String **
String hql="";
2. create Query object using session method
Query q=ses.createQuery(hql);

3. execute Query using method list


List<__> output=q.list();

4. print result
sysout(ouput);

Page 2 of 10
by RAGHU SIR[NARESH IT, HYD]

Page 3 of 10
by RAGHU SIR[NARESH IT, HYD]

pom.xml
<properties>
<maven.compiler.source>13</maven.compiler.source>
<maven.compiler.target>13</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.10.Final</version>
</dependency>
<dependency>

Page 4 of 10
by RAGHU SIR[NARESH IT, HYD]

<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
</dependencies>

Model class
package in.nit.model;

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

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@Entity
@Table(name="emptab")
@NoArgsConstructor
@AllArgsConstructor
public class Employee {
@Id
@Column(name="eid")
private Integer empId;
@Column(name="ename")
private String empName;
@Column(name="esal")
private Double empSal;
}

Page 5 of 10
by RAGHU SIR[NARESH IT, HYD]

hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>
<session-factory>
<property
name="hibernate.connection.driver_class">com.mysql.jdbc.Driv
er</property>
<property
name="hibernate.connection.url">jdbc:mysql://localhost:3306/
hibs</property>
<property
name="hibernate.connection.username">root</property>
<property
name="hibernate.connection.password">root</property>

<property
name="hibernate.dialect">org.hibernate.dialect.MySQL55Dialec
t</property>
<property name="hibernate.show_sql">true</property>
<property
name="hibernate.format_sql">false</property>
<property
name="hibernate.hbm2ddl.auto">update</property>

<mapping class="in.nit.model.Employee"/>
</session-factory>
</hibernate-configuration>

TestSave
package in.nit.test;

import org.hibernate.Session;
import org.hibernate.Transaction;

import in.nit.model.Employee;

Page 6 of 10
by RAGHU SIR[NARESH IT, HYD]

import in.nit.util.HibernateUtil;

public class TestInsert {

public static void main(String[] args) {


Session ses=HibernateUtil.getSf().openSession();
Transaction tx=null;
try(ses) {
tx=ses.beginTransaction();

Employee e1=new Employee(10, "A", 3.3);


Employee e2=new Employee(11, "B", 4.3);
Employee e3=new Employee(12, "C", 5.3);

ses.save(e1);
ses.save(e2);
ses.save(e3);

tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
}
}
}

Test Fetch All columns


package in.nit.test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.Query;

import in.nit.model.Employee;
import in.nit.util.HibernateUtil;

public class TestFetchAll {

Page 7 of 10
by RAGHU SIR[NARESH IT, HYD]

public static void main(String[] args) {


Session ses=HibernateUtil.getSf().openSession();
//Tx not required? select operation
try(ses) {
//String hql="select e from
in.nit.model.Employee e";
//String hql=" from
"+Employee.class.getName();

//1. Define one HQL String


String hql=" from in.nit.model.Employee ";

//2. create query object


Query q=ses.createQuery(hql);

//3. execute query


List<Employee> output=q.list();

//4. print result


output.forEach(System.out::println);

} catch (Exception e) {
e.printStackTrace();
}
}
}

Test Fetch one column


package in.nit.test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.Query;

import in.nit.model.Employee;
import in.nit.util.HibernateUtil;

Page 8 of 10
by RAGHU SIR[NARESH IT, HYD]

public class TestFetchOne {

public static void main(String[] args) {


Session ses=HibernateUtil.getSf().openSession();
//Tx not required? select operation
try(ses) {
//String hql="select empName from
in.nit.model.Employee";
String hql="select empName from " +
Employee.class.getName();

Query q=ses.createQuery(hql);

List<String> output=q.list();

output.forEach(System.out::println);

} catch (Exception e) {
e.printStackTrace();
}
}
}

Test Fetch Multiple columns


package in.nit.test;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.query.Query;

import in.nit.util.HibernateUtil;

public class TestFetchMultiple {

public static void main(String[] args) {


Session ses=HibernateUtil.getSf().openSession();
//Tx not required? select operation

Page 9 of 10
by RAGHU SIR[NARESH IT, HYD]

try(ses) {
String hql="select empId,empName from
in.nit.model.Employee ";

Query q=ses.createQuery(hql);

List<Object[]> output= q.list();

for(Object[] ob:output) {
System.out.println(ob[0]+"-"+ob[1]);
}

} catch (Exception e) {
e.printStackTrace();
}
}
}

Hibernate util
package in.nit.util;

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

public class HibernateUtil {

private static SessionFactory sf=null;

static {
sf=new Configuration()
.configure()
.buildSessionFactory();
}

public static SessionFactory getSf() {


return sf;
}
}

Page 10 of 10

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy