Versiones comparadas

Clave

  • Se ha añadido esta línea.
  • Se ha eliminado esta línea.
  • El formato se ha cambiado.

Para poder inicializar atributos de manera lazy con Hibernate, debemos permitir el byte code enhancement, añadiendo el siguiente plugin a nuestronuestro pom.xml:

Bloque de código
languagexml
<plugin>
  <groupId>org.hibernate.orm.tooling</groupId>
  <artifactId>hibernate-enhance-maven-plugin</artifactId>
  <version>${hibernate.version}</version>
  <executions>
    <execution>
      <configuration>
        <failOnError>true</failOnError>
        <enableLazyInitialization>true</enableLazyInitialization>
        <enableDirtyTracking>true</enableDirtyTracking>
        <enableAssociationManagement>true</enableAssociationManagement>
        <enableExtendedEnhancement>false</enableExtendedEnhancement>
      </configuration>
      <goals>
        <goal>enhance</goal>
      </goals>
    </execution>
  </executions>
</plugin>

...

Por último, en cada getter que queramos que tenga inicialización lazy tendremos que añadir la etiquetaetiqueta @Basic(fetch = FetchType.LAZY). Esto hará que cuando pidamos un objeto, éste se devuelva por defecto sin cargar el valor para ese campo. Una vez se llame al getter, se pedirá a base de datos su valor.

...