Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moving Hibernate instrumentation to build time

I have an application with about 3000 entities (I know it's a lot but I can't change it). When the application loads it takes Hibernate a minutes to do all the instrumentation and the SessionFactory setup stuff.
I was wondering if I can configure Hibernate to do the instrumentation on the original classes during build time.
This way I can avoid 3000 additional generated proxy classes and the huge overhead on application start-up.
I've found some information on Hibernate build time instrumentation (org.hibernate.tool.instrument.javassist.InstrumentTask) but it isn't clear whether this replaces totally the run-time instrumentation or only handles the Hibernate lazy property fetching mechanism.
Any information on how to move the proxy generation to build time will be appreciated.

like image 636
Avner Levy Avatar asked Oct 05 '12 13:10

Avner Levy


3 Answers

Yes, you can. There is an Ant task in the Hibernate code : org.hibernate.tool.instrument.javassist.InstrumentTask.

<target name="instrument" depends="compile">
    <taskdef name="instrument" classname="org.hibernate.tool.instrument.javassist.InstrumentTask">
        <classpath refid="<some-ant-path-including-hibernate-core-jar>"/>
        <classpath path="<your-classes-path>"/>
    </taskdef>

    <instrument verbose="true">
        <fileset dir="<your-classes>">
            <include name="*.class"/>
        </fileset>
    </instrument>
</target>

I have seen some Maven based ones as well.

like image 88
Steve Ebersole Avatar answered Nov 13 '22 15:11

Steve Ebersole


Since Hibernate 4.2.8 you can use the hibernate-enhance-maven-plugin:

<build>
    <plugins>
        <plugin>
            <groupId>org.hibernate.orm.tooling</groupId>
            <artifactId>hibernate-enhance-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>process-test-resources</phase>
                    <goals>
                        <goal>enhance</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
like image 4
Vlad Mihalcea Avatar answered Nov 13 '22 15:11

Vlad Mihalcea


Found the solution on the internet. Tried it rapidly, and it seems to work. Hope I am not to late..

The idea is to use maven-antrun-plugin. You need to had this in your pom.xml in the build/plugins section.

In the exemple bellow, don't forget: - to replace the ${hibernate.version} and ${javassist.version} with the version you are using. - to modify the include rules to have InstrumentTask runs only on your entity

<plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <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="**/model/**/*.class"/>
                                </fileset>
                            </instrument>
                        </tasks>
                    </configuration>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>org.hibernate</groupId>
                    <artifactId>hibernate-core</artifactId>
                    <version>${hibernate.version}</version>
                </dependency>

                <dependency>
                    <groupId>javassist</groupId>
                    <artifactId>javassist</artifactId>
                    <version>${javassist.version}</version>
                </dependency>
            </dependencies>
        </plugin>
like image 3
Samuel Avatar answered Nov 13 '22 14:11

Samuel