What do I have to change to avoid Hibernate giving me lazy javassist instance proxies rather than the true entity?
UPDATE: I am using Spring 3.x and Hibernate 4.x
The API I am using to load the entity is org.hibernate.internal.SessionImpl#load(Person.class, Id)
and the mapping simply:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.perfectjpattern.example.model">
<class name="Person" table="PERSON_" >
<id name="id">
<generator class="native"></generator>
</id>
<property name="name" update="false" />
<property name="age" update="true" />
</class>
<query name="Person.findByName">
<![CDATA[select p from Person p where p.name = ? ]]>
</query>
<query name="Person.findByAge">
<![CDATA[select p from Person p where p.age = :Age ]]>
</query>
</hibernate-mapping>
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. Simply put, Hibernate subclasses our entity class, using the CGLib library.
Explanation: load() method returns proxy object. load() method should be used if it is sure that instance exists.
LazyInitializer is a Hibernate component that helps to generate proxy objects for Entities and can also be used to fetch the underlying entities of these proxy objects.
The JPA lazy loading mechanism can either be implemented using Proxies or Bytecode Enhancement so that calls to lazy associations can be intercepted and relationships initialized prior to returning the result back to the caller.
Use get() rather than load().
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