Lazy loading doesn't work for @Formula in Hibernate

Viewed 1292

I'm trying to make one of my fields calculable, but I don't really want to calculate it all the time the entity is retrieved. What I want, is to calculate it only in case it's necessary for the current query or just for the time getter is invoked. That's why I use @Formula:

@Basic(fetch = FetchType.LAZY)
@Formula("(SELECT max(myEntity.CREATION_TIME) FROM MyEntity myEntity 
WHERE myEntity.account_id = id)")
private LocalDateTime entitiesModifiedDate;

To make it work, I use bytecode instrument like this:

        <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>

There are two problems that I face here:

  1. It seems the syntax is incorrect even though I got this from another question. The error I have:

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

  1. In case I remove verbose="true", it works, however, the subquery is a part of each query to my entities(no effect).

Is there anything else that should be done to make it work?

Here what I took as an example: http://tricksdev.blogspot.ru/2009/03/hibernate-bytecode-instrumentation.html

Updated:

Query example:

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
1 Answers
Related