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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:49:24+00:00 2026-05-23T12:49:24+00:00

I have the following scenario (in Java / Hibernate): I have two entity classes:

  • 0

I have the following scenario (in Java / Hibernate):

  • I have two entity classes: X and Y. X has a @ManyToOne association to Y which is not cascaded.
  • I create an (unmanaged) instance x of X and an (unmanaged) instance y of Y, and fill the reference to y in x. The only field of y that is filled is the primary key.
  • Entity y already has a corresponding row in the underlying database, but entity x is new.
  • I persist entity x.

When I perform this scenario, I expect to see one query: INSERT x. However, what actually happens is that Hibernate performs TWO queries:

  • SELECT y
  • INSERT x

Furthermore, I also notice that after the persist of x, the reference to y does not actually become managed and there is no instance of Y in the session! So, why is the SELECT on y performed at all? Are there ways to prevent this behaviour?

  • 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-23T12:49:24+00:00Added an answer on May 23, 2026 at 12:49 pm

    You don’t need to (actually you shouldn’t) instantiate Y manually. You can do a variant of this (depending on your configuration)

    Y y = (Y) session.load(Y.class, pk);
    

    This doesn’t retrieve Y from database, instead it loads a proxy consisting only from the pk you mentioned.

    Then assigning y to x, and persisting x would behave as you expect.

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

Sidebar

Related Questions

I have question about try, catch and finally in Java. Consider the following scenario:
The scenario is the following. I have a plain text file which contains 2,000,000
I am rather noob at java jars deployment. I have following scenario: I have
Image the following scenario, You have 10 Java applications accessing a database server Each
The scenario is the following: I have a java package ( myapp.config ) that
Have the following scenario. I have a few form, which essentially have a few
I have the following scenario (note that activity A has launchMode=singleTop ): Activity A
I got stuck with the following scenario like I have two timers. If user
Consider the following scenario, a servlet has been written in Java and once you
I have following scenario: Lets say we have two different webparts operating on the

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.