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 4002332
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:00:51+00:00 2026-05-20T08:00:51+00:00

I’m using JPA 2 and Hibernate 3. I noticed that calling EntityManager.merge() causes a

  • 0

I’m using JPA 2 and Hibernate 3. I noticed that calling EntityManager.merge() causes a SELECT on every referenced entity in the object graph PLUS inner joins between some of them.

Suppose you want to merge() a FooBar.

@Entity
public class FooBar {
    @ManyToOne
    private Foo foo;

    @ManyToOne
    private Bar bar;
}

@Entity
public class Foo {
    @ManyToOne
    private Baz baz;
}

@Entity
public class Bar {
    @ManyToOne
    private Baz baz;
}

If you do, Hibernate will issue a SELECT for each of FooBar, Foo, and Bar, and two for Baz. Then, it will issue a SELECT for Foo joined with Baz and another for Bar joined with Baz. Since I just wanted to merge a FooBar, I was expecting a single SELECT from it, but I ended up with a whopping 7 SELECT’s!

First of all, is this normal? Second, if it is, is there a way to issue just a single SELECT?

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-05-20T08:00:52+00:00Added an answer on May 20, 2026 at 8:00 am

    You are calling merge(..) with a detached object (i.e. one that is not associated with the session). What hibernate does here is – it loads an entity with the id of the passed object from the database and:

    • if no record is found, inserts the passed object
    • if an object is found, transfers all the fields and returns the persistent object

    In the latter case a loading from the DB is involved, hence Hibernate needs to fetch the whole object. Hence the many selects.

    You can try fetchType=LAZY for the ManyToOne relations (they are eager by default).

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

Sidebar

Related Questions

I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.