SkyWalker
SkyWalker

Reputation: 14307

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>

Upvotes: 5

Views: 10437

Answers (3)

ivivi
ivivi

Reputation: 21

You can use Hibernate.initialize(obj) after session.load(id).

This method can instantly initialize your obj.

Upvotes: 1

SkyWalker
SkyWalker

Reputation: 14307

Actually solved it by simply changing the mapping to (see the default-lazy="false"):

<?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" default-lazy="false">

Upvotes: 0

Ryan Stewart
Ryan Stewart

Reputation: 128849

Use get() rather than load().

Upvotes: 5

Related Questions