4

The lazy attribute for property tag in hibernate allows to lazily load the property as per the link: http://docs.jboss.org/hibernate/orm/3.3/reference/en-US/html/mapping.html#mapping-declaration-property

lazy (optional - defaults to false): specifies that this property should be fetched lazily when the instance variable is first accessed. It requires build-time bytecode instrumentation.

But when I tried to set lazy=true for one of my property it is not loading it lazily in this example:

Hibernate Mapping file:

<hibernate-mapping package="org.hibernate.tutorial.domain">

    <class name="Event" table="EVENTS" select-before-update="true">
        <id name="id" column="EVENT_ID">
            <generator class="native" />
        </id>
        <property name="date" type="timestamp" column="EVENT_DATE" />
        <property name="title" lazy="true"/>

        <set name="participants" table="PERSON_EVENT" inverse="true">
            <key column="EVENT_ID" />
            <many-to-many column="PERSON_ID" class="Person" />
        </set>
    </class>

</hibernate-mapping>

Program:

public static void main(String[] args) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Event event = (Event) session.get(Event.class, 135L);
        session.getTransaction().commit();
        System.out.println(event);
        HibernateUtil.getSessionFactory().close();
    }

Query generated by hibernate:

Hibernate: select event0_.EVENT_ID as EVENT1_0_0_, event0_.EVENT_DATE as EVENT2_0_0_, event0_.title as title0_0_ from EVENTS event0_ where event0_.EVENT_ID=?

Please help me in understanding why the lazy is not working in this case?

3 Answers 3

6

With Hibernate 5, this can be done easily using bytecode enhancement.

First, you need to add the following Maven plugin:

<plugin>
    <groupId>org.hibernate.orm.tooling</groupId>
    <artifactId>hibernate-enhance-maven-plugin</artifactId>
    <version>${hibernate.version}</version>
    <executions>
        <execution>
            <configuration>
                <enableLazyInitialization>true</enableLazyInitialization>
            </configuration>
            <goals>
                <goal>enhance</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Then, you can simply annotate your entity properties with @Basic(fetch = FetchType.LAZY):

@Entity(name = "Event")
@Table(name = "event")
public class Event extends BaseEntity {

    @Type(type = "jsonb")
    @Column(columnDefinition = "jsonb")
    @Basic(fetch = FetchType.LAZY)
    private Location location;

    public Location getLocation() {
        return location;
    }

    public void setLocation(Location location) {
        this.location = location;
    }
}

When you fetch the entity:

Event event = entityManager.find(Event.class, 
    eventHolder.get().getId());

LOGGER.debug("Fetched event");
assertEquals("Cluj-Napoca", event.getLocation().getCity());

Hibernate is going to load the lazy property using a secondary select:

SELECT e.id AS id1_0_0_
FROM   event e
WHERE  e.id = 1

-- Fetched event

SELECT e.location AS location2_0_
FROM   event e
WHERE  e.id = 1
Sign up to request clarification or add additional context in comments.

10 Comments

Vlad, is it possible to do so without Maven? Maven is banned at our company :-( I also tried using hibernate.enhance.enable property but no result
Hibernate offers a Gradle plugin as well, for build-time instrumentation. However, you can also use runtime instrumentation as well.
This assumes you use EntityManager API instead of SessionFactory, correct? I have seen in the Hibernate code that instrumentation may also happen at runtime using properties, but code is referenced only in the EntityManager factory
This works with both EntityManagerFactory or SessionFactory. It doesn't matter. It's just that JPA/Hibernate entities are instrumented. Afterwards, they are used as such by EMF or SF.
Hibernate 5, what about hibernate 4 and 3 ? can this be achieved ?
|
3

Lazy loading is just a hint to your persistence provider. This hint does not provide any guarantees that the entities will be actually loaded lazily.

The provider is free to load them eagerly if this is determined to be a better approach by the provider.

Especially basic properties will rarely be loaded lazily, as it does not boost performance to load them lazily, rather the opposite.

The behaviour may vary depending on the context, so lazy loading is impossible to test for reliably. Eager loading (the default) on the other hand is guaranteed and can be tested for.

EDIT If you just want to see the effects of lazy loading - lazy loading is more likely to occur when the lazily loaded attributes are relations to other entities or LOBs.

2 Comments

Well, I have evidence in my project that eagerly-loading a VARCHAR(255) column on 1M+ entities streamed over a ScrollableResult hits performance dramatically. So there is sometimes a need to load basic properties lazily.
As long as the persistence provider accepts the hint and modifies the SQL query accordingly, this would make sense. I have made my recommendation based on the Pro JPA 2 book. It advises against lazy loading on basic props, probably because persistence providers did not modify the SQL when the book has been written. Have you by change logged the queries fired by JPA with and without the lazy hint? Some details would be interesting.
0

Lazy loading you have used <property name="title" lazy="true"/> is not right way of using it, because title is not associated with other objects. If it is used in your relationship mapping <set name="participants" table="PERSON_EVENT" inverse="true" lazy="true"> then it will give you some performance boost.

In the above configuration. If lazy="false" : - when you load the Event object that time child object Person is also loaded and set to setPerson() method. If you call evet.getPerson() then loaded data returns. No fresh database call.

If lazy="true" :- This the default configuration. If you don't mention then hibernate consider lazy=true. when you load the Event object that time child object Person is not loaded. You need extra call to data base to get address objects. If you call event.getPerson() then that time database query fires and return results. Fresh database call.

To test once set it false <set name="participants" table="PERSON_EVENT" inverse="false" lazy="true"> and then see your output query

Comments

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.