Home >Database >Mysql Tutorial >Why Does Lazy Loading Fail for @Lob Fields in Spring and Hibernate?

Why Does Lazy Loading Fail for @Lob Fields in Spring and Hibernate?

Susan Sarandon
Susan SarandonOriginal
2024-11-02 18:28:02428browse

Why Does Lazy Loading Fail for @Lob Fields in Spring and Hibernate?

Lazy Loading Issues with @Lob in Spring and Hibernate

In web applications utilizing Spring and Hibernate, database records containing file-related data are rendered on a web page. While the process operates smoothly when data volumes are modest, excessive data quantities trigger an "OutOfMemoryError."

As an initial troubleshooting step, setting the "blobField" properties to "null" resolved the memory issue, implying that lazy loading was ineffective despite the @Basic(fetch=FetchType.LAZY) annotation.

Explanations and Workarounds

According to Hibernate documentation, @Lob fields are inherently lazy and do not require explicit Lazy annotations. However, inconsistent behavior has been reported across databases and drivers.

One solution involves bytecode instrumentation techniques like Javassist or Cglib.

Recommended Solution

To address this issue reliably, it is suggested to restructure data mappings. Instead of utilizing properties, create "fake" one-to-one mappings. Remove LOB fields from the primary class, establish new classes referencing the same table and primary key but only containing the necessary LOB fields. Mark these mappings as one-to-one, fetch select, and lazy true. This approach ensures lazy loading while the parent object remains in the session.

The above is the detailed content of Why Does Lazy Loading Fail for @Lob Fields in Spring and Hibernate?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn