作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试使我的其中一个字段可计算,但我真的不想在检索实体时一直计算它。我想要的是仅在当前查询需要或仅在调用 getter 时计算它。这就是我使用@Formula 的原因:
@Basic(fetch = FetchType.LAZY)
@Formula("(SELECT max(myEntity.CREATION_TIME) FROM MyEntity myEntity
WHERE myEntity.account_id = id)")
private LocalDateTime entitiesModifiedDate;
为了让它工作,我使用这样的字节码工具:
<plugin>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.3</version>
<executions>
<execution>
<id>Instrument domain classes</id>
<configuration>
<tasks>
<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="**/entity/*.class" />
</fileset>
</instrument>
</tasks>
</configuration>
<phase>process-classes</phase>
<goals>
<goal>run</goal>
</goals>
</execution>
</executions>
</plugin>
我在这里面临两个问题:
An Ant BuildException has occurred: instrument doesn't support the "verbose" attribute: The type doesn't support the "verbose" attribute.
还有什么需要做的吗?
这里我举个例子:http://tricksdev.blogspot.ru/2009/03/hibernate-bytecode-instrumentation.html
更新:
查询示例:
SELECT
...{fields}...,
(
SELECT
MAX(event.creation_time)
FROM
MyEntity myEntity
WHERE
myEntity.accountId = account1_.id
) AS formula0_
FROM
Table1 table10_
CROSS JOIN account_table account1_
WHERE
table10_.account_id = account1_.id
AND
account1_.user_id = 1
最佳答案
使用 hibernate-enhance-maven-plugin ,或者尝试使用 maven-antrun-plugin 最新版本 (1.8)
<plugins>
<plugin>
<groupId>org.hibernate.orm.tooling</groupId>
<artifactId>hibernate-enhance-maven-plugin</artifactId>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>enhance</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
引用:https://docs.jboss.org/hibernate/orm/5.0/topical/html/bytecode/BytecodeEnhancement.html
关于java - 延迟加载不适用于 Hibernate 中的@Formula,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45300318/
我是一名优秀的程序员,十分优秀!