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

标签 java hibernate maven ant lazy-loading

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

关于java - 延迟加载不适用于 Hibernate 中的@Formula,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45300318/

相关文章:

java - Eclipse/RAD 运行菜单消失

java - android setAdapter 不触发 getView 或不构建 ListView

java - Eclipse 在获取 session 代码时暂停且没有断点

java - Selenium SouceLabs - java.lang.NoClassDefFoundError : org/apache/commons/codec/binary/Base64

java - Jadira 依赖导致 AbstractMethodError

java - 带有 struts 标签的输出参数化 bean 属性

java - Python(和Java)中最快的数据打包

java - PGpoint Hibernate(反)序列化异常

java - Hibernate Spring - @OneToMany - 外键未存储在数据库中

java - 如何通过 rpm-maven-plugin 使用运行时环境变量