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.