All I could gather from Google is that:
Hibernate uses a proxy object to implement lazy loading. When we request to load the Object from the database, and the fetched Object has a reference to another concrete object, Hibernate returns a proxy instead of the concrete associated object.
Hibernate creates a proxy object using bytecode instrumentation (provided by javassist). Hibernate creates a subclass of our entity class at runtime using the code generation library and replaces the actual object with the newly created proxy.
So, what exactly does the Proxy object contain?
Does it contain a skeleton object reference object with only the id field set? Others field will be set when we call the get method?
Does the Proxy object contain the JDBC statement to fetch all data required to fully populate the referenced object.
Is there something else I might be missing?
I am not asking for spoon feeding but if you can provide any link with information that would be great.
Any correction to above description will also be welcomed.
Example.
class Address { String city; String country; } class Person{ int id; String name; Address address; }
When we try to load the Person object, Hibernate will subclass Person class like:
class ProxyPerson extends Person { int id; String name; Address proxyCGLIBObject; }
and return a ProxyPerson object. Object of ProxyPerson will have a value for id and name but proxy for Address.
Am I correct?
What can I expect from adding a toString() method on the proxy object?
By definition, a proxy is “a function authorized to act as the deputy or substitute for another”. This applies to Hibernate when we call Session. load() to create what is called an uninitialized proxy of our desired entity class. This subclass will be the one to be returned instead of querying the database directly.
A proxy is an object that wraps another object maintaining its interface and optionally providing additional features. Proxies usually delegate behavior on the real object they are proxying but can execute code around the call to the wrapped object.
Proxy is a structural design pattern that provides an object that acts as a substitute for a real service object used by a client. A proxy receives client requests, does some work (access control, caching, etc.) and then passes the request to a service object.
Explanation: load() method returns proxy object. load() method should be used if it is sure that instance exists.
The Hibernate Proxy is used to substitute an actual entity POJO (Plain Old Java Object).
The Proxy class is generated at runtime and it extends the original entity class.
Hibernate uses Proxy objects for entities is for to allow [lazy loading][1].
When accessing basic properties on the Proxy, it simply delegates the call to the original entity.
Every List
, Set
, Map
type in the entity class is substituted by a PersistentList
, PersistentSet
, PersistentMap
. These classes are responsible for intercepting a call to an uninitialized collection.
The Proxy doesn't issue any SQL statement. It simply triggers an InitializeCollectionEvent, which is handled by the associated listener, that knows which initialization query to issue (depends on the configured fetch plan).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With