延迟加载不适用于 Hibernate 中的@Formula

Lazy loading doesn't work for @Formula in Hibernate

我正在尝试使我的一个字段可计算,但我真的不想在检索实体时一直计算它。我想要的是仅在当前查询需要或仅在调用 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>

我在这里遇到两个问题:

  1. 虽然我是从另一个问题中得到的,但似乎语法不正确。我的错误:

An Ant BuildException has occurred: instrument doesn't support the "verbose" attribute: The type doesn't support the "verbose" attribute.

  1. 如果我删除 verbose="true",它会起作用,但是,子查询是对我的实体的每个查询的一部分(无效)。

还有什么需要做的才能让它发挥作用吗?

这里以我为例: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