Hibernate Query Language: by Raghu Sir (Naresh It, Hyd)
Hibernate Query Language: by Raghu Sir (Naresh It, Hyd)
Hibernate Query Language: by Raghu Sir (Naresh It, Hyd)
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]
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;
ses.save(e1);
ses.save(e2);
ses.save(e3);
tx.commit();
} catch (Exception e) {
tx.rollback();
e.printStackTrace();
}
}
}
import java.util.List;
import org.hibernate.Session;
import org.hibernate.query.Query;
import in.nit.model.Employee;
import in.nit.util.HibernateUtil;
Page 7 of 10
by RAGHU SIR[NARESH IT, HYD]
} catch (Exception e) {
e.printStackTrace();
}
}
}
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]
Query q=ses.createQuery(hql);
List<String> output=q.list();
output.forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.util.List;
import org.hibernate.Session;
import org.hibernate.query.Query;
import in.nit.util.HibernateUtil;
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);
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;
static {
sf=new Configuration()
.configure()
.buildSessionFactory();
}
Page 10 of 10