Java, .NET, PHP, PYTHON, ANGULAR, .NET, ML, Data Scince, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

 Join in Hibernate:-

Join is used to merge the columns, in the case of Hibernate we will create a relationship or mapping concept to perform join operation:-

One to Many

Many to One

Many to Many

One to One

Example of Join Syntax:-




Vendor

vid
vname
Set childern  ----> store record of child object

Cutsomer
custid
custname
vid(fk)  ----> it will take record which will be exist under master

Syntax of Left Join

select v.vname,v.vid,c.custid,c.custname from Vendor v left join v.children c.

Vendor information will match from Customer

Syntax of Right Join
select v.vname,v.vid,c.custid,c.custname from Vendor v right join v.children c.

Customer information will match from Vendor

Many to one :-

Pojo clas

Vendor:-
vid
vname

Customer
cid
cname
parentObjects

Right Join:-

select v.vname,v.vid,c.cname from Customer c Right Join c.parentObjects;

vendor table info will be matched from Customer Table

Left Join:-
select v.vname,v.vid,c.cname from Customer c Left Join c.parentObjects;

Customer Table info will be matched from Vendor Table

Inner Join:-
select v.vname,v.vid,c.cname from Customer c Inner Join c.parentObjects;

Customer Table Matched Information will be Show from Vendor name.

..................................................
Assignment:-
Project:-
ProjectId
ProjectName
ProjectDesc
...................................................................................................................................................
ProjectAssignment
Developerid
Developername
Projectid

One to many:-

Inner Join

Right Join

Left Join
many to one
Inner Join
Right Join
Left Join


Complete Sample example:-

package mappingser;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.*;
import org.hibernate.cfg.*;
import mapping.*;
/**
 * Servlet implementation class ManyToOneSer
 */
@WebServlet("/ManyToOneSer")
public class ManyToOneSer extends HttpServlet {
private static final long serialVersionUID = 1L;
     
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ManyToOneSer() {
        super();
        // TODO Auto-generated constructor stub
    }


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType("text/html");
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");

SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.openSession();



Query q = session.createQuery("select v.vendroid,v.verndroname,c.customername from Customer c  Inner Join c.parentObjects v");
List lst = q.list();
Iterator it = lst.iterator();
while(it.hasNext())
{
Object arr[] = (Object[]) it.next();
    out.print(arr[0]+" "+arr[1]+" "+arr[2]+"<br>");
}
session.close();
    out.println("One To Many is Done..!!");
    factory.close();

}


}


Many To One Join Example:-

package uao;

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 JoinMappingFile {

public static void main(String[] args) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
        SessionFactory sf = cfg.buildSessionFactory();
        Session s =sf.openSession();
    //    Query q = s.createQuery("select v.vid,v.vname,c.customername from Customer c inner join c.parentObjects v");
    //    Query q = s.createQuery("select v.vid,v.vname,c.customername from Customer c left join c.parentObjects v");
        Query q = s.createQuery("select v.vid,v.vname,c.customername from Customer c right join c.parentObjects v");
        List lst = q.list();
        Iterator it = lst.iterator();
        while(it.hasNext())
        {
        Object arr[]=  (Object[])it.next();
        System.out.println(arr[0]+" "+arr[1]+" "+arr[2]);
        }
        s.close();

}

}


Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post