我有一个包含大约 3000 个实体的应用程序(我知道它很多,但我无法更改它)。当应用程序加载时,Hibernate需要几分钟来执行所有检测和SessionFactory设置工作。
我想知道我是否可以配置 Hibernate 以在构建时对原始类进行检测。
这样,我可以避免 3000 个额外的生成代理类和应用程序启动时的巨大开销。
我找到了一些关于 Hibernate 构建时检测 ( org.hibernate.tool.instrument.javassist.InstrumentTask
) 的信息,但不清楚这是完全取代运行时检测还是只处理 Hibernate 懒惰属性获取机制。
有关如何将代理生成移动到构建时的任何信息将不胜感激。
是的,你可以。 休眠代码中有一个 Ant 任务: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>
我也看过一些基于Maven的。
从 Hibernate 4.2.8 开始,您可以使用 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>
在互联网上找到了解决方案。快速尝试,似乎有效。希望我不要迟到。
这个想法是使用maven-antrun-plugin。你需要在你的pom中拥有它.xml在构建/插件部分。
在示例波纹管中,不要忘记: - 将 ${hibernate.version} 和 ${javassist.version} 替换为您正在使用的版本。 - 修改包含规则以使 InstrumentTask 仅在您的实体上运行
<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>