Hibernate Left Join, Hibernate Left Join Example
Left join means, the objects from both sides of the join are selected and more objects from left side are selected, even though no equal objects are there at right side, no confusion you will be able to understand if you go through this example i guess ?
Let us see an example on hibernate left join, am taking one-to-many to explain this concept files required….
- Vendor.java
- Customer.java
- Vendor.hbm.xml
- Customer.hbm.xml
- hibernate.cfg.xml
- urLogic.java
Vendor.java
123456789101112131415161718192021222324252627282930package str; import java.util.Set; public class Vendor { private int vendorId; private String vendorName; private Set children; public int getVendorId() { return vendorId; } public void setVendorId(int vendorId) { this.vendorId = vendorId; } public String getVendorName() { return vendorName; } public void setVendorName(String vendorName) { this.vendorName = vendorName; } public Set getChildren() { return children; } public void setChildren(Set children) { this.children = children; } }
Customer.java
12345678910111213141516171819202122232425262728package str; public class Customer { private int customerId; private String customerName; private int forevenId; public int getCustomerId() { return customerId; } public void setCustomerId(int customerId) { this.customerId = customerId; } public String getCustomerName() { return customerName; } public void setCustomerName(String customerName) { this.customerName = customerName; } public int getForevenId() { return forevenId; } public void setForevenId(int forevenId) { this.forevenId = forevenId; } }
Vendor.xml
1234567891011121314151617181920<?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="str.Vendor" table="vendor"> <id name="vendorId" column="vendid" /> <property name="vendorName" column="vendname" length="10"/> <set name="children" cascade="all" lazy="false"> <key column="forevenid" /> <one-to-many class="str.Customer"/> </set> </class> </hibernate-mapping>
Customer.java
123456789101112131415<?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="str.Customer" table="customer"> <id name="customerId" column="custid" /> <property name="customerName" column="custname" length="10"/> <property name="forevenId" column="forevenid" insert="false" /> </class> </hibernate-mapping>
hibernate.cfg.xml
123456789101112131415161718192021<?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="connection.driver_class">oracle.jdbc.driver.OracleDriver </property> <property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property> <property name="connection.username">system</property> <property name="connection.password">admin</property> <property name="dialect">org.hibernate.dialect.OracleDialect</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="Customer.hbm.xml"></mapping> <mapping resource="Vendor.hbm.xml"></mapping> </session-factory> </hibernate-configuration>
urLoic.java
12345678910111213141516171819202122232425262728293031323334package str; 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 urLogic { public static 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.vendorName, c.customerName from Vendor 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:
- Actually in urLogic.java see line number 27, i have written select v.vendorName, c.customerName from Vendor v Left Join v.children c
- See before Left Join key word, i selected from Vendor v, after Left Join keywork v.children c
- Means, actually we know in one to many if we select the data from parent then, we will get the data automatically from the children table, i mean records mapping with the parent table, hope you remembered ?
- Let in parent we have 5 records, and in children we have 3 records. And 2 records are having relation with children table records
-
And thats it mates.., try with Right Join and remaining joins just same concept.
post a comment