Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to avoid getting javassist lazy Entity proxy instances in Hibernate

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>
like image 549
SkyWalker Avatar asked Dec 24 '12 17:12

SkyWalker


People also ask

What is proxy in Hibernate lazy loading?

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.

Which method returns proxy object in Hibernate?

Explanation: load() method returns proxy object. load() method should be used if it is sure that instance exists.

What is lazy initialization in Hibernate?

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.

What is JPA proxy?

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.


1 Answers

Use get() rather than load().

like image 81
Ryan Stewart Avatar answered Oct 23 '22 11:10

Ryan Stewart