我有一个在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类以及私有字段和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
接口和FieldHandler
字段,因为我不确定这是否有必要,之后我不再在启动时获得'HHH000157: Lazy property fetching available for:'
message并且它返回到默认情况下急切加载。
我在这里错过了什么吗? 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>
qazxsw poi在你的persistence.xml中需要qazxsw poi
懒惰,返回请求引用时加载的真实对象(此选项必须使用字节码增强,如果类没有增强,则返回PROXY)除非您无法负担代理的使用,否则应避免使用此选项
@LazyToOne(LazyToOneOption.NO_PROXY)
像往常一样,这是一个配置问题,似乎ant运行插件需要指向包含实体而不是父目录之一的确切目录
这对我有用......
<property name="hibernate.ejb.use_class_enhancer" value="true"/>
你应该伪造一对多的关系。这将起作用,因为延迟加载集合比单个可空属性的延迟加载容易得多,但是如果使用复杂的JPQL / HQL查询,通常这种解决方案非常不方便。
另一种是使用构建时间字节码检测。有关更多详细信息,请阅读Hibernate文档:19.1.7。使用lazy属性获取。请记住,在这种情况下,您必须将@LazyToOne(LazyToOneOption.NO_PROXY)注释添加到一对一关系中以使其变得懒惰。将提取设置为LAZY是不够的。
最后一个解决方案是使用运行时字节码检测,但它只适用于在完整的JEE环境中使用Hibernate作为JPA提供程序的用户(在这种情况下,将“hibernate.ejb.use_class_enhancer”设置为true应该可以解决这个问题:实体管理器配置或者使用Hibernate和Spring配置来进行运行时编织(这可能很难在一些较旧的应用程序服务器上实现)。在这种情况下,还需要@LazyToOne(LazyToOneOption.NO_PROXY)注释。