使用@LazyToOne进行Hibernate延迟加载(LazyToOneOption.NO_PROXY) [英] Hibernate Lazy Loading with @LazyToOne(LazyToOneOption.NO_PROXY)

查看:580
本文介绍了使用@LazyToOne进行Hibernate延迟加载(LazyToOneOption.NO_PROXY)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app running Hibernate 4.2.21 on JBoss AS 7.2

我们目前有一些@OneToOne关系,由于已知的延迟加载限制,总是急切地反向获取。

We currently have a few @OneToOne relationships which, due to known limitations of lazy loading, will alway fetch eagerly on the inverse side.

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

In order to enable Lazy loading for inverse relationships I am trying to enable build-time bytecode instrumentation.

这就是我的意思到目前为止已完成...

Here's what I've done so far...

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

1) Activate instrumentation using maven-antrun-plugin (I tried the hibernate-enhance-maven-plugin and couldn't get it working but thats for another question), I now get the following maven output in the build log:

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

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

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

2) Next I annotated all @OneToOne relationships as follows...

    @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)然后我添加实现FieldHandled 到@Entity类以及私有字段和getter和setter:

3) Then I add implement FieldHandled to the @Entity classes along with private field and getter and setter:

private FieldHandler fieldHandler;

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

success...I am now getting following output in the deployment log:

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。

The relationships now no longer eagerly load...but they don't lazy load either, they just return null silently.

我试过删除 FieldHandled 界面来自实体的 FieldHandler 字段,因为我不确定是否有必要,之后我不再获得'HHH000157:Lazy property fetching available for :'在启动时消息,它会在默认情况下回到急切加载状态。

I have tried removing the FieldHandled interface and FieldHandler field from the Entity as I was not sure if this was necessary, after this I no longer get the 'HHH000157: Lazy property fetching available for:'message at startup and it goes back to eagerly loading by default.

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

Am I missing something here? The hibernate docs are not explicit on how to actually set this up

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

Added the Ant task config as per comments:

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


推荐答案

像往常一样,这是配置问题,似乎ant运行插件需要指向包含实体而不是父目录之一的确切目录

As usual it was a configuration issue, it seems that the ant run plugin needs to be pointed to an exact directory that contains the entities rather than one of the parent directories

这对我有用...

<instrument verbose="true">
    <fileset dir="${project.build.directory}/classes/uk/co/myapp/entities">
        <include name="*.class" />
    </fileset>
</instrument>

这篇关于使用@LazyToOne进行Hibernate延迟加载(LazyToOneOption.NO_PROXY)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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