gpt4 book ai didi

java - Hibernate 使用 @LazyToOne(LazyToOneOption.NO_PROXY) 进行延迟加载

转载 作者:行者123 更新时间:2023-12-01 19:11:16 28 4
gpt4 key购买 nike

我有一个在 JBoss AS 7.2 上运行 Hibernate 4.2.21 的应用程序

我们目前有一些 @OneToOne 关系,由于 known limitations of lazy loading ,总是会在反面急切地获取。

为了启用反向关系的延迟加载,我正在尝试启用构建时字节码检测。

这是我到目前为止所做的...

1) 使用 maven-antrun-plugin 激活检测(我尝试了 hibernate-enhance-maven-plugin,但无法让它工作,但这是另一个问题),我现在在构建日志中得到以下 Maven 输出:

[INFO] --- maven-antrun-plugin:1.7:run (Instrument domain classes) @ MyApp-entities ---
[INFO] Executing tasks

instrument:
[instrument] starting instrumentation
[INFO] Executed tasks

2)接下来我将所有@OneToOne关系注释如下......

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "client", optional=false)
@LazyToOne(LazyToOneOption.NO_PROXY)
public ClientPrefs getClientPrefs() {
return clientPrefs;
}

public void setClientPrefs(ClientPrefs clientPrefs) {
this.clientPrefs = clientPrefs;
}

3) 然后我添加 implement FieldHandled到 @Entity 类以及私有(private)字段、getter 和 setter:

private FieldHandler fieldHandler;

成功...我现在在部署日志中得到以下输出:

15:54:09,720 INFO  [org.hibernate.tuple.entity.EntityMetamodel] (ServerService Thread Pool -- 56) HHH000157: Lazy property fetching available for: uk.co.myapp.entities.Session
15:54:09,730 INFO [org.hibernate.tuple.entity.EntityMetamodel] (ServerService Thread Pool -- 57) HHH000157: Lazy property fetching available for: uk.co.myapp.entities.Session
15:54:09,969 INFO [org.hibernate.tuple.entity.EntityMetamodel] (ServerService Thread Pool -- 56) HHH000157: Lazy property fetching available for: uk.co.myapp.entities.Client
15:54:09,970 INFO [org.hibernate.tuple.entity.EntityMetamodel] (ServerService Thread Pool -- 57) HHH000157: Lazy property fetching available for: uk.co.myapp.entities.Client
15:54:09,999 INFO [org.hibernate.tuple.entity.EntityMetamodel] (ServerService Thread Pool -- 56) HHH000157: Lazy property fetching available for: uk.co.myapp.entities.Country
15:54:10,003 INFO [org.hibernate.tuple.entity.EntityMetamodel] (ServerService Thread Pool -- 57) HHH000157: Lazy property fetching available for: uk.co.myapp.entities.Country
15:54:10,054 INFO [org.hibernate.tuple.entity.EntityMetamodel] (ServerService Thread Pool -- 56) HHH000157: Lazy property fetching available for: uk.co.myapp.entities.Pool
15:54:10,054 INFO [org.hibernate.tuple.entity.EntityMetamodel] (ServerService Thread Pool -- 57) HHH000157: Lazy property fetching available for: uk.co.myapp.entities.Pool
15:54:10,569 INFO [org.hibernate.tuple.entity.EntityMetamodel] (ServerService Thread Pool -- 56) HHH000157: Lazy property fetching available for: uk.co.myapp.entities.User
15:54:10,624 INFO [org.hibernate.tuple.entity.EntityMetamodel] (ServerService Thread Pool -- 57) HHH000157: Lazy property fetching available for: uk.co.myapp.entities.User

关系现在不再急切加载...但它们也不会延迟加载,它们只是默默地返回 null。

我尝试删除 FieldHandled接口(interface)和FieldHandler来自实体的字段,因为我不确定这是否有必要,此后我不再得到 'HHH000157: Lazy property fetching available for:'启动时显示消息,默认情况下会返回到急切加载。

我在这里遗漏了什么吗? hibernate 文档并未明确说明如何实际设置

编辑:根据评论添加 Ant 任务配置:

<build>
<plugins>
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.7</version>
<executions>
<execution>
<phase>process-classes</phase>
<id>Instrument domain classes</id>
<configuration>
<target name="instrument">
<taskdef name="instrument"
classname="org.hibernate.tool.instrument.javassist.InstrumentTask">
<classpath>
<path refid="maven.dependency.classpath" />
<path refid="maven.plugin.classpath" />
</classpath>
</taskdef>
<instrument verbose="true">
<fileset dir="${project.build.outputDirectory}">
<include name="MyApp-entities/co/uk/myapp/entities/*.class" />
</fileset>
</instrument>
</target>
</configuration>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.21.Final</version>
</dependency>

<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>

最佳答案

@LazyToOne(LazyToOneOption.NO_PROXY)需要<property name="hibernate.ejb.use_class_enhancer" value="true"/>在你的 persistance.xml 中

懒惰,在请求引用时返回加载的真实对象(此选项强制执行字节码增强,如果类未增强,则回退到 PROXY)应避免此选项,除非您无法承担使用代理的费用

NO_PROXY option

关于java - Hibernate 使用 @LazyToOne(LazyToOneOption.NO_PROXY) 进行延迟加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45107084/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com