@Basic(fetch = FetchType.LAZY) does not work?

前端 未结 7 1640
执念已碎
执念已碎 2020-12-17 09:17

I use JPA (Hibernate) with Spring. When i want to lazy load a Stirng property i use this syntax:

@Lob
@Basic(fetch = FetchType.LAZY)
public String getHtmlSum         


        
相关标签:
7条回答
  • 2020-12-17 09:41

    Use FieldHandled with @Basic(fetch=FetchType.LAZY) works:

    public class myFile implements Serializable, FieldHandled
    {
    
        private FieldHandler      fieldHandler;
    
        @Lob
        @Basic(fetch = FetchType.LAZY)
        @Column(name = "CONTENT")
        protected byte[]          content;
    
    0 讨论(0)
  • 2020-12-17 09:43

    Lazy Lob loading would require bytecode instrumentation to work properly, so it is not available by default in any JPA implementation I'm aware of.

    Your best bet is to put the Lob into a separate entity, like HtmlSummary, and use a lazily loaded one-to-one association.

    0 讨论(0)
  • 2020-12-17 09:50

    from the specification of JPA they say that even if you use annotate a property to be fetched lazily, this is not guaranteed to be applied, so the properties may or may not be loaded lazily (depends on the implementer of JPA), however if you specify that you should fetch them Eagerly then the JPA implementer must load them eagerly.

    Bottom line: @Basic(fetch = FetchType.LAZY) may or may not work, depends on the JPA implementer.

    0 讨论(0)
  • 2020-12-17 09:57

    First of all, you should know that the JPA specs clearly specifies that LAZY is only a hint to JPA providers, so it's not a mandatory requirement.

    For basic type lazy fetching to work, you need to enable bytecode enhancement and explicitly set the enableLazyInitialization configuration property to true:

    <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>
    
    0 讨论(0)
  • 2020-12-17 09:58

    I think that it would be similar to EclipseLink, there you need to have enabled weaving otherwise, the fetch setting takes no effect. The weaving requires bytecode access. This could help: https://stackoverflow.com/a/18423704/7159396

    0 讨论(0)
  • 2020-12-17 09:59

    Lazy fetching only applies to references to other entities or collections of entities. It does not apply to values like String or int.

    0 讨论(0)
提交回复
热议问题