Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate proxy generation

I was going through the documentation for hibernate and found these lines

The no-argument constructor is a requirement for all persistent classes; Hibernate has to create objects for you, using Java Reflection. The constructor can be private, however package or public visibility is required for runtime proxy generation and efficient data retrieval without bytecode instrumentation

Can anyone please explain the runtime proxy generation and efficient data retrieval without bytecode instrumention

like image 898
Bharat Bisht Avatar asked May 31 '15 05:05

Bharat Bisht


1 Answers

Runtime proxy means that Hibernate will wrap your class with a Proxy class. You can see in debugger, that instantiated objects are not of your type but of some proxy one.

To do so, Hibernate needs to override your class. The parameterless constructor is needed to call base() constructor. Hibernate doesn't know how to fill your custom parameters. Other think is to make all your properties and methods virtual so they can be overridden too.

Think of it like you have third party library (one containing your persistent classes) and now you need to add some general functionality to them, without reading the doc and analyzing class by class, property by property.

like image 100
Jan Zahradník Avatar answered Sep 29 '22 13:09

Jan Zahradník