InterviewSolution
| 1. |
What is Hibernate Proxy and how does it impact loading? |
|
Answer» A PROXY is defined as a function which acts as a substitute to another function. When a load() method is called on session, a proxy is returned and this proxy CONTAINS the ACTUAL method to load the data. The session.load() method creates an uninitialized proxy object for our desired entity class. The proxy implementation delegates all property methods except for the @id to the session which will in turn populate the instance. When an entity method is called, the entity is loaded and the proxy becomes an initialized proxy object. Let us understand this with an example. Let us assume that there is an entity called Student. Let us also assume that initially, this entity has no connection or relation with any other tables except the Student table. When the session.load() method is called to instantiate the Student, Student dia = session.load(Student.class, newLong(1));Hibernate will create an uninitialized proxy for this entity with the id we have assigned it and will contain no other property as we have not yet communicated with the database. However, when a method is called on dia, String firstName = dia.getFirstName();Hibernate will fire a query to the database for information on the student with primary key 1 and populates the object dia with the PROPERTIES from the corresponding row. If the row is not found, Hibernate throws an ObjectNotFoundException |
|