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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:26:23+00:00 2026-06-02T20:26:23+00:00

Let assume that I have a relation Master Slave. In the database exists one

  • 0

Let assume that I have a relation Master Slave. In the database exists one row in the table Master with id=1.
Why does this following code work:

    Slave slave = new Slave();
    slave.setId(1L);

    Master master = new Master();
    master.setId(1L);
    slave.setMaster(master);
    em.persist(slave);

So Master is detached entity and after commit refence between slave and master is created. Why? I thougt that in thikinf of case we have to read the Master row from the database by “find” function.
Where can I read more about it?

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

    Welcome to the dark land of Hibernate relations. First of all if you want to learn what is going on add to your hibernate.cfg.xml

    <property name="show_sql">true</property>
    

    Thanks to that you will see in console all SQL queries performed by Hibernate for you – that’s the best way to figure out how Hibernate works in a given moment.

    Back to your unexpected result. The important thing is who owns relationship. In your case I guess that in Slave table there is foreign key that points to Master table primary key (obviously you can use also join table – does not matter).

    Hibernate, when creating relationship, cares only about that side, which owns relationship – in your case, it is enough to save information about new relation in Slave table. In fact Master does not need to know about relation, so it is not necessary to call save on that entity. That’s why your code works.

    Helpful read:

    • http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch01.html#tutorial-associations
    • http://www.mkyong.com/hibernate/inverse-true-example-and-explanation/ – pay attention on meaning of inverse attribute
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's assume that I have a table in a mysql database as follows, date
Let's assume that I have following models: class ScoutBook(models.Model): troop = models.ForeignKey('Dictionary', limit_choices_to={'type' :
Let's assume that we have following jQuery plugins (each of them in separate files):
let's assume that I have some codes like this. What should I write there
Let's assume that I have following class template: <> - read as template template<template<class
Let's assume that we have simple jQuery code like the following: var $document =
Let's assume that I have a table with two columns A and B. There
Let us assume that we have a simple UI that has only one state
Let's assume that I have a method in class like this: - (void)doSomethingOnlyWhenNeeded {
Let's assume that I have two tables... Foo and Bar. They contain the following

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.