我试图让我的一个字段可以计算,但我并不真正想要在实体被检索的时候计算它。我想要的是,仅在当前查询需要时或仅在调用getter时才计算它。这就是为什么我使用@Formula:延迟加载在@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>
有迹象表明,我在这里面临着两个问题:
- 似乎即使我从另一个问题得到这个语法,语法也是不正确的。错误我:
的蚂蚁BuildException发生:仪器不支持“详细”属性:类型不支持“详细”属性。
- 万一我除去冗长=“真”,它的工作原理,但是,子查询是每个查询,以我的实体(无效果)的一部分。
还有什么应该做的,使其工作?
这里我把为例: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
你能通过git分享这个项目的一部分吗? – Sergii