5

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>
4
  • 2
    Use eager rather than lazy loading. Commented Dec 24, 2012 at 17:07
  • I can imagine, but what exactly and where needs to be configured, that's the OP Commented Dec 24, 2012 at 17:09
  • If you show us one of your mapped entities we can help you. Commented Dec 24, 2012 at 17:09
  • 3
    Almost 100% of the time that you want to do this, it's the wrong way to solve the problem you're having. You might consider asking for help solving your actual problem instead. Commented Dec 24, 2012 at 17:32

3 Answers 3

5

Use get() rather than load().

Sign up to request clarification or add additional context in comments.

3 Comments

+1 for the extra detail but I think load would be more flexible as in other cases I may choose to lazy load. I can this way configure it for every Entity separately.
Your application should be choosing when to fetch thing eagerly and when not. I.e. use get() when you have to and load() any other time.
But also note that if your application really needs to be aware of this, then you're almost certainly doing something wrong. Lazy loading is designed to be transparent.
1

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

This method can instantly initialize your obj.

2 Comments

...which is basically a more complicated and slower version of using session.get().
you can initialize your obj when you required rather than initialize it always.
0

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">

2 Comments

That's pretty dangerous because it sets all entities and collections to be non-lazy, meaning you could easily load the entire database into memory by accident.
Thank you for bringing this up, however, as you can note in the OP I have one only entity defined in my mapping. In my use-case I also load one element only (by Id). If I maintain a larger project each entity will have its own mapping file where I can decide what the loading policy should be. There is also the other side of the coin where for different use-cases I have to invoke either load or get this is not a good thing from the code reusability standpoint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.