1

For example, I have the following db of two tables: Home and Person. There is one-to-many relationship between these tables. In one home many people live.

Now, let assume the following situation. I load one person from a db as a result I load a home in which this person lives. Then I load another person which lives in the same home. The question is what I am loading: a new home object or the same object which was loaded for the first person? Why?

1 Answer 1

1

The same Home object. Hibernate keeps track, by PRIMARY KEY, of which objects it already has on hand, and when the Person constructor comes to fill in the Home, a reference to the already existing object will be used.

On the other hand, you should note that Home maybe getting fetched "lazily", in which case only a proxy (placeholder) for this Home will be a field in both Persons.

Sign up to request clarification or add additional context in comments.

2 Comments

How long hibernate stores this history? What is the scope?
Suggest you read up on Hibernate Sessions and Transactions. In this case, Session lifetime unless you do some detaching and re-attaching by hand.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.