Example on the Left Join in the Hibernate by R4R Team

Here in the Hibernate Left Join Means is that we can take object from both sides of the join which are selected and more objects from

Here in the Hibernate Left Join Means is that we can take object from both sides of the join which are selected and more objects from left side are selected, whenever equal object are there at right side, so here it can not make more confusion to make the understand.

 

So for understanding the Left Join in Hibernate we go through a simple example to understand the properly what is Left join and how works the left Join. For this example we take some pages to go through the example which are given below:

 

1. Seller.java

2. Customer.java

3. Seller.hbm.java

4. hibernate.cfg.xml

5. CreationLogic.java

 

1. Seller.java

 

package r4r;

import java.util.Set;

public class Seller {

privateint sellerId;

private String sellerName;

privateSet children;

publicint getSellerId() {

returnsellerId;

}

publicvoid setSellerId(int sellerId) {

this.sellerId = sellerId;

}

public String getSellerName() {

returnsellerName;

}

publicvoid setSellerName(String sellerName) {

this.sellerName = sellerName;

}

publicSet getChildren() {

returnchildren;

}

publicvoid setChildren(Set children) {

this.children = children;

}

}

 

2. Customer.java

 

package r4r;

public class Customer {

privateint customerId;

private String customerName;

privateint forevenId;

publicint getCustomerId() {

returncustomerId;

}

publicvoid setCustomerId(int customerId) {

this.customerId = customerId;

}

public String getCustomerName() {

returncustomerName;

}

publicvoid setCustomerName(String customerName) {

this.customerName = customerName;

}

publicint getForevenId() {

returnforevenId;

}

publicvoid setForevenId(int forevenId) {

this.forevenId = forevenId;

}

}

 

3. Seller.java

 

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="r4r.Seller" table="seller">

<id name="sellerId"column="sellerid" />

<property name="sellerName"column="sellername"length="50"/>

<set name="children"cascade="all"lazy="false">

<key column="forevenid" />

<one-to-many class="r4r.Customer"/>

</set>

</class>

</hibernate-mapping>

 

4. Customer.java

<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="r4r.Customer"table="customer">

<id name="customerId"column="custid" />

<property name="customerName"column="custname"length="60"/>

<property name="forevenId"column="forevenid"insert="false" />

</class>

</hibernate-mapping>

 

5. hibernate.cfg.xml

 

<?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.0.dtd">

<hibernate-configuration>

<session-factory>

<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>

<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>

<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_pro</property>

<property name="hibernate.connection.username">root</property>

<property name="hibernate.connection.username">root</property>

<mapping resource="Customer.hbm.xml"></mapping>

<mapping resource="Vendor.hbm.xml"></mapping>

</session-factory>

</hibernate-configuration>

 

6. CreationLogic.java

 

package r4r;

import java.util.Iterator;

import java.util.List;

import org.hibernate.Query;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class CreationLogic {

publicstatic void main(String args[])

{

Configuration cfg = new Configuration();

cfg.configure("hibernate.cfg.xml");

SessionFactory factory = cfg.buildSessionFactory();

Session session = factory.openSession();

Query qry= session.createQuery("select v.sellerName, c.customerName from Seller v Left Join v.children c");

List l = qry.list();

Iterator it=l.iterator();

while(it.hasNext())

{

Object rows[] = (Object[])it.next();

System.out.println(rows[0]+" -- " +rows[1]);

}

}

}

 

Explanation of the above code in details:

 

1. Here in the given code we have told about the use of the Left Join. In code we use select v.sellerName, c.customerName from Seller v Left Join v.children c .

 

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!