Question

Lets say there is an entity Student which has a one to many relationship with an entity Subject which is lazily loaded. @OneToMany annotation is used to specify the relationship. I would like to understand the internal workings of hibernate on how this annotation is handled.

Was it helpful?

Solution

When creating an instance of Student from the data read in the database, Hibernate initializes the subjects collection with an instance of its own implementation of Collection (which well' call PersistentCollection).

This PersistentCollection is not initialized initially, and doesn't contain any data. The first time any method of this collection is called, a SQL query is executed to load all the subjects of the student, and the subjects are then cached inside the collection for all subsequent method calls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top