Reputation: 3032
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?
Upvotes: 1
Views: 33
Reputation: 19362
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.
Upvotes: 1