@Basic(fetch = FetchType.LAZY) 不起作用? [英] @Basic(fetch = FetchType.LAZY) does not work?

查看:39
本文介绍了@Basic(fetch = FetchType.LAZY) 不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spring 中使用 JPA(休眠).当我想延迟加载一个 Stirng 属性时,我使用以下语法:

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 getHtmlSummary() {
    return htmlSummary;
}

但是当我查看 hibernate 创建的 sql 时,似乎这个属性不是延迟加载的?我也在 ANT 脚本中使用这个类 org.hibernate.tool.instrument.javassist.InstrumentTask 来检测这个属性,但它似乎不起作用.

But when i look at the sql that hibernate creates, it seems this property is not lazy loaded? I also use this class org.hibernate.tool.instrument.javassist.InstrumentTask in ANT script to instrument this property but it seems it does not work.

请帮帮我.

科斯罗.

推荐答案

首先,你应该知道 JPA 规范明确规定 LAZY 只是对 JPA 提供者的一个提示,所以它不是强制性要求.

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.

要使基本类型的延迟获取工作,您需要启用字节码增强 并将enableLazyInitialization 配置属性显式设置为true:

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>

这篇关于@Basic(fetch = FetchType.LAZY) 不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆