Using lazy for properties in Hibernate

前端 未结 3 1315
一个人的身影
一个人的身影 2021-01-03 05:41

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.htm

相关标签:
3条回答
  • 2021-01-03 06:19

    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

    0 讨论(0)
  • 2021-01-03 06:26

    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.

    0 讨论(0)
  • 2021-01-03 06:40

    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
    
    0 讨论(0)
提交回复
热议问题