简体   繁体   中英

Using lazy for properties in Hibernate

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?

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM