0

I try to make JPA Entity's one of the members lazy-fetched in spring boot while using MySQL DB as data source (this might not be mandatory but I'd rather have it mentioned). My entity looks like this:

@Entity
public class DiskFile {

    @Id
    @GeneratedValue (strategy = GenerationType.IDENTITY)
    private long id;
    @NotEmpty
    @Column(unique = true)
    private String path;
    @Lob
    @Column (columnDefinition = "LONGBLOB")
    @Basic (fetch = FetchType.LAZY)
    private byte[] data;

    public DiskFile() {

    }


    /* Getters and Setters */
}

As you'd guessed I want data member to be fetched with lazy pattern, because it usually contains megabytes of data. Situation gets even more dire when I try to fetch list of these entities and all of them need to fetch megabytes and megabytes of data.

I know that there are several questions like these on Stackoverflow and I tried solutions mentioned there. I already know that for hibernate that annotation is only like a recommendation and he might forcefully try to fetch that member Eagerly. So I have added following line to my application.properties:

spring.jpa.open-in-view = false

And I have already added following plugin into my maven build configurations:

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

however I don't get any lazy-fetching with that solutions, since I can see megabytes of data flowing into my system after calling appropriate API endpoint for this entity.

5
  • 1
    I faced a similar problem. The fact that I was using an IDE for debugging, and it was using the toString() method on the entity to show information about it, made it look like the field was being eagerly loaded every time when in fact it wasn't. Actually it was being fetched immediately by the IDE call to the toString() method. To check if that is your case remove, in case you have it, the lazy field from toString() or check for a similar case.
    – harogaston
    Commented Jul 24, 2019 at 3:06
  • 1
    If spring.jpa.open-in-view = false doesn't cause a bunch of LazyInitializationExceptions upon calling the endpoint, but the problem still persists, it means one of two things: (1) FetchType.LAZY is not respected at all, (2) something is triggering the loading while still within the transaction boundary. To verify (1), set a breakpoint right after the entity is being retrieved and check if the DiskFile.data is present (it should be null at that point). (2) is a little tricky, the best course of action would be to enable Hibernate logging to see what queries get executed and when
    – crizzis
    Commented Jul 24, 2019 at 9:10
  • @crizzis logging is enabled and as I can see query doesn't respect FetchType.LAZY Commented Jul 25, 2019 at 7:57
  • @harogaston I don't use toString() method. For most part I just pass that object into ReponseEntity and I have JsonIgnore attribute set at that member, so it should not feel obliged to get member value. Commented Jul 25, 2019 at 8:00
  • Huh, that's... weird. Can you confirm lazy loading works for other @Basic properties (e.g. name)? Maybe the plugin execution does not trigger for some strange reason. If all else fails, you can try the fake one-to-one workaround described here
    – crizzis
    Commented Jul 25, 2019 at 9:20

0

Your Answer

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