0% found this document useful (0 votes)
14 views9 pages

HQL Joins

The document provides an overview of HQL joins and includes Java code for defining model classes for Employee and Address entities, along with a Hibernate configuration file. It also contains a test class demonstrating how to create and query these entities using Hibernate. Additionally, it includes contact information for further inquiries.

Uploaded by

madhu
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)
14 views9 pages

HQL Joins

The document provides an overview of HQL joins and includes Java code for defining model classes for Employee and Address entities, along with a Hibernate configuration file. It also contains a test class demonstrating how to create and query these entities using Hibernate. Additionally, it includes contact information for further inquiries.

Uploaded by

madhu
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/ 9

-by RAGHU [SATHYATECHNOLOGIES, AMEERPET, HYD]

HQL Joins - by RAGHU


*)HQL Joins Syntax:
select pobj.variables,cobj.variables
from ParentClass pobj
JOIN TYPE
pobj.childHASAVariable as cobj
where condition;

output tables:

child model class:

package com.app.model;

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

@Entity
@Table(name="addrtab")
1
-by RAGHU [SATHYATECHNOLOGIES, AMEERPET, HYD]

public class Address {


@Id
@Column(name="aid")
private int addrId;
@Column(name="loc")
private String loc;
@Column(name="pin")
private int pinCode;

public int getAddrId() {


return addrId;
}
public void setAddrId(int addrId) {
this.addrId = addrId;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
public int getPinCode() {
return pinCode;
}
public void setPinCode(int pinCode) {
this.pinCode = pinCode;
}
@Override
public String toString() {
return "Address [addrId=" + addrId + ", loc=" + loc + ", pinCode=" +
pinCode + "]";
}

parent model class:


2
-by RAGHU [SATHYATECHNOLOGIES, AMEERPET, HYD]

package com.app.model;

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

@Entity
@Table(name="emptab")
public class Employee {
@Id
@Column(name="eid")
private int empId;
@Column(name="ename")
private String empName;
@Column(name="esal")
private double empSal;

//Employee *...1 Address


@ManyToOne
@JoinColumn(name="aidFk")
private Address addr; //HAS-A

public int getEmpId() {


return empId;
}

public void setEmpId(int empId) {


this.empId = empId;
}

3
-by RAGHU [SATHYATECHNOLOGIES, AMEERPET, HYD]

public String getEmpName() {


return empName;
}

public void setEmpName(String empName) {


this.empName = empName;
}

public double getEmpSal() {


return empSal;
}

public void setEmpSal(double empSal) {


this.empSal = empSal;
}

public Address getAddr() {


return addr;
}

public void setAddr(Address addr) {


this.addr = addr;
}

@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName +
", empSal=" + empSal + ", addr=" + addr + "]";
}

4
-by RAGHU [SATHYATECHNOLOGIES, AMEERPET, HYD]

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/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>

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

<mapping class="com.app.model.Address"/>
<mapping class="com.app.model.Employee"/>

</session-factory>
</hibernate-configuration>

HibernateUtil:

5
-by RAGHU [SATHYATECHNOLOGIES, AMEERPET, HYD]

package com.app.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;
}

Test class:

package com.app.test;

import java.util.List;

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

import com.app.util.HibernateUtil;

6
-by RAGHU [SATHYATECHNOLOGIES, AMEERPET, HYD]

public class Test {

public static void main(String[] args) {


Transaction tx=null;
try(Session ses=HibernateUtil.getSf().openSession()){
//Step#1 run this first to create tables with data
/*tx=ses.beginTransaction();

Address addr1=new Address();


addr1.setAddrId(1);
addr1.setLoc("HYD");
addr1.setPinCode(500);

Address addr2=new Address();


addr2.setAddrId(2);
addr2.setLoc("BAN");
addr2.setPinCode(600);

Address addr3=new Address();


addr3.setAddrId(3);
addr3.setLoc("CHN");
addr3.setPinCode(700);

Address addr4=new Address();


addr4.setAddrId(4);
addr4.setLoc("DHL");
addr4.setPinCode(800);

Address addr5=new Address();


addr5.setAddrId(5);
addr5.setLoc("BMB");
addr5.setPinCode(900);

Employee emp1=new Employee();


emp1.setEmpId(10);
emp1.setEmpName("A");

7
-by RAGHU [SATHYATECHNOLOGIES, AMEERPET, HYD]

emp1.setEmpSal(1.1);

Employee emp2=new Employee();


emp2.setEmpId(11);
emp2.setEmpName("B");
emp2.setEmpSal(2.2);

Employee emp3=new Employee();


emp3.setEmpId(12);
emp3.setEmpName("C");
emp3.setEmpSal(3.3);

Employee emp4=new Employee();


emp4.setEmpId(13);
emp4.setEmpName("D");
emp4.setEmpSal(4.4);

Employee emp5=new Employee();


emp5.setEmpId(14);
emp5.setEmpName("E");
emp5.setEmpSal(5.5);

//links
emp2.setAddr(addr2);
emp4.setAddr(addr4);

ses.save(addr1);
ses.save(addr2);
ses.save(addr3);
ses.save(addr4);
ses.save(addr5);

ses.save(emp1);
ses.save(emp2);
ses.save(emp3);
ses.save(emp4);

8
-by RAGHU [SATHYATECHNOLOGIES, AMEERPET, HYD]

ses.save(emp5);

tx.commit();*/

String hql=" select emp.empName,addr.loc "


+ " from com.app.model.Employee emp "
+ " full join "
+ " emp.addr as addr ";
Query q=ses.createQuery(hql);
List<Object[]> list=q.list();
for(Object[] ob:list) {
System.out.println(ob[0]+","+ob[1]);
}

} catch (Exception e) {

e.printStackTrace();
}

}
}

FB Group:

https://www.facebook.com/groups/thejavatemple/

email:

[email protected]

You might also like