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

  • SEARCH
  • Home
  • 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 7849049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:24:40+00:00 2026-06-02T18:24:40+00:00

Short Version Hibernate is doing multiple select queries instead of only two to retrieve

  • 0

Short Version

Hibernate is doing multiple select queries instead of only two to retrieve a one to one relationship

Long Version

Person Class:

@Entity
@Table(name = "people")
public class Person{

  @Id
  @GeneratedValue
  @Column
  private Integer id;

  @Column
  private String name;

  @OneToOne(optional=false, fetch=FetchType.EAGER)
  private Job job;
}

Job Class:

@Entity
@Table(name = "jobs")
public class Job{

  @Id
  @GeneratedValue
  @Column
  private Integer id;

  @Column
  private String name;
}

Displaying all People and their Jobs:

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Person> people = session.createQuery("from Person").list();
for(Person p : people){
    System.out.println(p.getName() + " | " + p.getJob().getName());
}

What I expected: Two selects, one for the five People and one for their three different Jobs (three people have the same job)

What I got: Four selects, one for People and three for each of their different Jobs

Hibernate: select person0_.id as id1_, person0_.job_id as job3_1_, person0_.name as name1_ from people person0_
Hibernate: select job0_.id as id0_0_, job0_.name as name0_0_ from jobs job0_ where job0_.id=?
Hibernate: select job0_.id as id0_0_, job0_.name as name0_0_ from jobs job0_ where job0_.id=?
Hibernate: select job0_.id as id0_0_, job0_.name as name0_0_ from jobs job0_ where job0_.id=?
Jim | Programmer
Andrew | DBA
Tomas | Sysadmin
Henry | Sysadmin
Jerry | Sysadmin

If I had 1000 people with 1000 different jobs, I would see 1000 selects being executed. How can I make it so Hibernate only does two selects?

Thanks

  • 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-02T18:24:41+00:00Added an answer on June 2, 2026 at 6:24 pm

    This is an example of the n + 1 problem, for which the Hibernate manual suggests using the JOIN fetch mode. You can enable this with the hibernate-specific annotation

    @Fetch(FetchMode.JOIN)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

short version: how can I get rid of the multiple-versions-of-python nightmare ? long version:
Short version: How do I create the <> column manually? Long version: In Access
Short version: assuming I don't want to keep the data for long, how do
Short version: How can I map two columns from table A and B if
Short version : How can I drag Link 1 into one of the dropdowns
Short version: What kind of escape sequence can one use to search for unicode
Short version: After inserting a select element with options into the DOM with an
Short Version Are class variables thread-safe for the duration of a controller action? Long
Short version: How do I write this query in squeel? SELECT OneTable.*, my_count FROM
Short version: When two lists are adjacent to each other, is there a way

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.