Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7785481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:15:46+00:00 2026-06-01T20:15:46+00:00

I am working on a application where I have to join 3-4 tables for

  • 0

I am working on a application where I have to join 3-4 tables for a select operation.
I am hibernate 3 with annotations and added my class structure below.however I have tried using many variations in the Join fetch strategy,but not able to get the result in 2 selects. Please go through and try to help me out.
Candidates class

@JsonAutoDetect
@Entity
@Table(name="HC_RESUME_BANK")
public class Candidates{

private int id;
private String firstName;
private String lastName;
private String email;
private String phone;
@JsonUnwrapped  
private Set<CandidateStatus> candidateStats = new HashSet<CandidateStatus>(0);

@OneToMany  //(mappedBy="candidate")
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ResID",nullable=false,referencedColumnName="RID")
@JsonIgnore
public Set<CandidateStatus> getCandidateStats() {
        return candidateStats;
}
public void setCandidateStats(Set<CandidateStatus> candidateStats) {
    this.candidateStats = candidateStats;
}

CandidateStatus class

@JsonAutoDetect
@Entity

@Table(name="HC_REQ_RESUME")

public class CandidateStatus{

private int id;
private String statusTitle;
@JsonUnwrapped
private Candidates candidate;
private int reqid;
private int resid;

@JsonUnwrapped
private Requirements requirement;

@ManyToOne
@Fetch(FetchMode.JOIN)

@JoinColumn(name="ReqID",insertable=false,updatable=false)
//@JsonIgnore
public Requirements getRequirement() {
    return requirement;
}
    @ManyToOne //(fetch =    FetchType.LAZY,cascade=CascadeType.ALL,targetEntity=Candidates.class)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ResID",insertable=false,updatable=false)
//@ForeignKey(name = "FK_ResID")

public Candidates getCandidate() {
    return candidate;
}

public void setCandidate(Candidates candidate) {
    this.candidate = candidate;
}

Clients class

 @JsonAutoDetect
 @Entity
 @Table(name="HC_CLIENTS")
 public class Clients{

private int id;
private String clientName;
@JsonUnwrapped
private Set<Requirements> require= new HashSet<Requirements>(0);
//private List<Requirements> require;


@OneToMany  //(fetch=FetchType.EAGER)
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ClientID",referencedColumnName="RID")


@JsonIgnore
public Set<Requirements> getRequire() {
    return require;
}
public void setRequire(Set<Requirements> require) {
    this.require = require;
}

and finally Requirements class

@JsonAutoDetect
@Entity

@Table(name="HC_REQUISITIONS")
public class Requirements{

private int id;
private int clientId;
private String reqTitle;
@JsonUnwrapped
private Clients client;

@ManyToOne
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ClientID",insertable=false,updatable=false)
//@JsonIgnore
public Clients getClient() {
    return client;
}
public void setClient(Clients client) {
    this.client = client;
}
    @OneToMany
@Fetch(FetchMode.JOIN)
@JoinColumn(name="ReqID",referencedColumnName="RID")
@JsonIgnore
public Set<CandidateStatus> getCandidateStatus() {
    return candidateStatus;
}
public void setCandidateStatus(Set<CandidateStatus> candidateStatus) {
    this.candidateStatus = candidateStatus;
}

I have eliminated the Getter/Setter methods for avoiding unnessary code.
What is wrong in the above association of classes due to which N+1 occurs?
Query is:

    return hibernateTemplate.find("from Requirements as req order by req.reqTitle");

I want to fetch only Requirement class’ objects but I get all the objects and a N+1 result of queries

Thank you for your time
Sachin

@RaulGogo: Tried the criteria query but the problem persists.These are the log instances of the query.And I added @BatchSize(size=100) which reduces the no of queries by doing batch fetch.Any thing that I have missed in terms of annotation config?

    Hibernate: 
    /* criteria query */ select
    this_.RID as RID3_3_,
    this_.ClientID as ClientID3_3_,
    this_.ReqTitle as ReqTitle3_3_,
    candidates2_.ReqID as ReqID3_5_,
    candidates2_.RID as RID5_,
    candidates2_.RID as RID1_0_,
    candidates2_.ResID as ResID1_0_,
    candidates2_.reqid as reqid1_0_,
    candidates2_.ReqID as ReqID1_0_,
    candidates2_.resid as resid1_0_,
    candidates2_.StatusTitle as StatusTi4_1_0_,
    candidates3_.RID as RID0_1_,
    candidates3_.EmailID as EmailID0_1_,
    candidates3_.FirstName as FirstName0_1_,
    candidates3_.LastName as LastName0_1_,
    candidates3_.Mobile as Mobile0_1_,
    clients4_.RID as RID2_2_,
    clients4_.ClientName as ClientName2_2_ 
from
    HC_REQUISITIONS this_ 
left outer join
    HC_REQ_RESUME candidates2_ 
        on this_.RID=candidates2_.ReqID 
left outer join
    HC_RESUME_BANK candidates3_ 
        on candidates2_.ResID=candidates3_.RID 
left outer join
    HC_CLIENTS clients4_ 
        on this_.ClientID=clients4_.RID 
order by
    this_.ReqTitle asc

  Hibernate: 
  /* load one-to-many com.options.model.Candidates.candidateStats */ select
    candidates0_.ResID as ResID0_3_,
    candidates0_.RID as RID3_,
    candidates0_.RID as RID1_2_,
    candidates0_.ResID as ResID1_2_,
    candidates0_.reqid as reqid1_2_,
    candidates0_.ReqID as ReqID1_2_,
    candidates0_.resid as resid1_2_,
    candidates0_.StatusTitle as StatusTi4_1_2_,
    requiremen1_.RID as RID3_0_,
    requiremen1_.ClientID as ClientID3_0_,
    requiremen1_.ReqTitle as ReqTitle3_0_,
    clients2_.RID as RID2_1_,
    clients2_.ClientName as ClientName2_1_ 
from
    HC_REQ_RESUME candidates0_ 
left outer join
    HC_REQUISITIONS requiremen1_ 
        on candidates0_.ReqID=requiremen1_.RID 
left outer join
    HC_CLIENTS clients2_ 
        on requiremen1_.ClientID=clients2_.RID 
where
    candidates0_.ResID=?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:15:47+00:00Added an answer on June 1, 2026 at 8:15 pm

    Try using criteria.

    return getHibernateTemplate().execute(new HibernateCallback<List<Requirement>>() {
            @Override
            public List<Requirement>doInHibernate(Session session) throws HibernateException, SQLException {
                return (List<Requirement>) session.createCriteria(Requirement.class)
                        .addOrder(Order.asc("reqTitle"))
                        .list();
            }
        });
    

    This does a select * from Requirement order by reqTitle asc and it will return a list of Requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a join for an application I am working on. SELECT community.id,
At the company i am working on we have an application build on Jboss/Apache/Hibernate
I have a working application using python and zeromq and I would like to
I am working on an application an have an issue about running shell command
I am new to wpf and i am working on first application i have
I am working on splitting out an existing, working application that I currently have
I have an application I have been working on and it can be slow
I'm working on an application where users have to make a call and type
I am working on an application where I have an images folder relative to
I'm working on an application that will have attachments, and I would like to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.