如何使用Hibernate JPA和Spring Framework启用加载时间/运行时编织 [英] How to enable load time / runtime weaving with Hibernate JPA and Spring Framework

查看:106
本文介绍了如何使用Hibernate JPA和Spring Framework启用加载时间/运行时编织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Hibernate作为JPA提供者(我使用它的 EntityManagerFactory 而不是它的 SessionFactory )在Spring框架应用程序中。我设法让Spring Framework的加载时间编织支持起作用,所以我超过了这个障碍。



我需要启用的延迟加载byte [ ] @ManyToOne 属性。我知道如何使用Hibernate的ant任务在 build 时间内对我的实体进行测试(编织),但是我希望将我的实体放在运行时中进行加载(加载时编织)。我在几个Google搜索结果中看到了参考,但没有提供启用它的实际说明。我需要设置什么样的属性来指示Hibernate,它可以在运行时测试我的实体? 解决方案

调试,我明白了这一点。 Hibernate ORM文档不包含这些信息令人遗憾。 (公平地说, Hibernate EntityManager文档确实存在,但不容易找到。关于使用lazy property fetching的Hibernate指令只说:懒惰属性加载需要构建时间字节码工具。它并没有提及你可以使用运行时检测工具。 )



您必须做的第一件事是设置hibernate.ejb.use_class_enhancer JPA属性为true String )。这告诉Hibernate它可以通过在 PersistenceUnitInfo 实例上调用 addTransformer 来使用应用服务器类转换。 应用程序服务器类转换实际上是Spring的 LoadTimeWeaver 。如果您使用的是Spring的Java配置和 LocalContainerEntityManagerFactoryBean ,并且Hibernate是编译时依赖项,您可以使用 AvailableSettings.USE_CLASS_ENHANCER 常量,而不是字符串文字hibernate.ejb.use_class_enhancer(这可能会导致错字)



如果您使用Spring的Java配置,则需要执行额外的步骤,直到 SPR-10856 是固定的。 LocalContainerEntityManagerFactoryBean setLoadTimeWeaver 方法不会自动调用,因此您必须手动调用它。在 @Configuration 类中,只需 @Inject @Autowire a LoadTimeWeaver 实例,并在创建 LocalContainerEntityManagerFactoryBean setLoadTimeWeaver

通过这些步骤,我现在在Tomcat中使用Spring框架中的Hibernate运行时实体字节码工具。


I'm using Hibernate as a JPA provider (I'm using its EntityManagerFactory instead of its SessionFactory) in a Spring Framework application. I managed to get Spring Framework's load time weaving support working, so I'm past that hurdle.

I need to enable lazy loading of byte[] and @ManyToOne properties on an entity. I understand how to instrument (weave) my entities at build time using Hibernate's ant task, but I'd like to instrument my entities at runtime instead (load time weaving). I've seen references to in on several Google search results, but no actual instructions for enabling it. What property do I need to set to instruct Hibernate that it can instrument my entities at runtime?

解决方案

After considerable reading of code and debugging, I figured this out. It's a shame the Hibernate ORM documentation doesn't include this information. (To be fair, the Hibernate EntityManager documentation does, but it's not easily found. The Hibernate instructions on "Using lazy property fetching" only says, "Lazy property loading requires buildtime bytecode instrumentation." It does not mention that you can use runtime instrumentation with a Hibernate EntityManager property.)

The first thing you must do is set the "hibernate.ejb.use_class_enhancer" JPA property to "true" (String). This tells Hibernate that it may use the "application server" class transformation by calling addTransformer on the PersistenceUnitInfo instance. The "application server" class transformation is really Spring's LoadTimeWeaver. If you are using Spring's Java configuration and LocalContainerEntityManagerFactoryBean, and Hibernate is a compile-time dependency, you could use the AvailableSettings.USE_CLASS_ENHANCER constant instead of the string-literal "hibernate.ejb.use_class_enhancer" (which would make it typo-resistant).

If you are using Spring's Java configuration, there is an additional step you must take until SPR-10856 is fixed. LocalContainerEntityManagerFactoryBean's setLoadTimeWeaver method is not called automatically like it should be, so you must call it manually. In your @Configuration class, just @Inject or @Autowire a LoadTimeWeaver instance and call setLoadTimeWeaver manually when you are creating the LocalContainerEntityManagerFactoryBean.

With these steps taken, I'm now using Hibernate's runtime entity bytecode instrumentation with Spring Framework in Tomcat.

这篇关于如何使用Hibernate JPA和Spring Framework启用加载时间/运行时编织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆