-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Join for 3-4 tables
PostPosted: Tue Feb 07, 2012 5:35 am 
Newbie

Joined: Wed Jan 25, 2012 1:59 am
Posts: 8
Hello
I have an existing database using which i have to build an app.The use case for my problem needs a query that fetches data from three to four tables.
This will need a join which I am aware of,but as a newbie to hibernate I am unable to fix it.I have worked with associations on 2 tables but not more than that or complex joins also.

The table structure is as follows:

Code:
Table1         Table2                             Table3           Table4
Id                Id                                  Id                  Id
field1           field1(PK to Table1)           field1             field1 (PK to table 2)
                                                                            field2 (PK to table 3)

I have achieved it using SQL like this:
Select * from T1 inner join T2 on T1.id=T2.field1 inner join T2.id=T4.field1 inner join T3.id=T4.field2;

I am aware inner join and on in HQL but some work around or solution for this using Annotations.

Please suggest some solutions as i did not find anything similar on the web.

Regards
Sachin


Top
 Profile  
 
 Post subject: Re: Join for 3-4 tables
PostPosted: Tue Feb 07, 2012 7:16 am 
Newbie

Joined: Thu Jan 27, 2011 10:53 am
Posts: 12
Hi,
May you join your mapping (xml or annotated class)? It will be of great help to provide an answer.
Join query can be implicit by using manytoone, onetoone mapping.


Top
 Profile  
 
 Post subject: Re: Join for 3-4 tables
PostPosted: Wed Feb 08, 2012 12:58 am 
Newbie

Joined: Wed Jan 25, 2012 1:59 am
Posts: 8
Hello
Thanks for the reply.I am pasting the annotated classes and an image of the schema design.I have implemented a join for two of the tables,but no idea how it can be done for more tables.Also,my database is a existing one,and i don't want to use an extra association table(that is created during association) or foreign key column created newly.Instead I want to use the existing primary/foreign key mapping.

These are the tables:
Candidates.java
Code:
package com.hibernate.candidate;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.CascadeType;


@Entity
@Table(name="HC_RESUME_BANK")
public class Candidates{
   
   private int id;
   private String firstName;
   private String lastName;
   private String email;
   
   private Set<CandidateStatus> candidateStats = new HashSet<CandidateStatus>(0);
      
   @OneToMany(mappedBy="candidate")
   @JoinColumn(name="ResID",nullable=false)
   public Set<CandidateStatus> getCandidateStats() {
      return candidateStats;
   }
   public void setCandidateStats(Set<CandidateStatus> candidateStats) {
      this.candidateStats = candidateStats;
   }
   
   
   
   
   @Id
   @Column(name="RID")
   public int getId() {
      return id;
   }
      
   public void setId(int id) {
      this.id = id;
   }
   
   @Column(name="FirstName")
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   
   @Column(name="LastName")
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   
   @Column(name="EmailID")
   public String getEmail() {
      return email;
   }
   public void setEmail(String email) {
      this.email = email;
   }
   
   
}


CandidateStatus.java
Code:
package com.hibernate.candidate;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.CascadeType;


@Entity
@Table(name="HC_RESUME_BANK")
public class Candidates{
   
   private int id;
   private String firstName;
   private String lastName;
   private String email;
   
   private Set<CandidateStatus> candidateStats = new HashSet<CandidateStatus>(0);
      
   @OneToMany(mappedBy="candidate")
   @JoinColumn(name="ResID",nullable=false)
   public Set<CandidateStatus> getCandidateStats() {
      return candidateStats;
   }
   public void setCandidateStats(Set<CandidateStatus> candidateStats) {
      this.candidateStats = candidateStats;
   }
   
   
   
   
   @Id
   @Column(name="RID")
   public int getId() {
      return id;
   }
      
   public void setId(int id) {
      this.id = id;
   }
   
   @Column(name="FirstName")
   public String getFirstName() {
      return firstName;
   }
   public void setFirstName(String firstName) {
      this.firstName = firstName;
   }
   
   @Column(name="LastName")
   public String getLastName() {
      return lastName;
   }
   public void setLastName(String lastName) {
      this.lastName = lastName;
   }
   
   @Column(name="EmailID")
   public String getEmail() {
      return email;
   }
   public void setEmail(String email) {
      this.email = email;
   }
   
   
}


Requirements.java

Code:
package com.hibernate.candidate;

import javax.persistence.Entity;
import javax.persistence.Table;


@Entity
@Table(name="HC_REQUISISTIONS")
public class Requirements {
   
   private int id;
   private String requirement;
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getRequirement() {
      return requirement;
   }
   public void setRequirement(String requirement) {
      this.requirement = requirement;
   }
   
}


[img]file:///E:/db_schema.jpg[/img]

Hope I am clear enough,if not please let me know.

Thanks
Sachin


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.