Early support for JPA 4.0 EntityAgent (autowiring a shared proxy)

As of Spring Framework 7.0.4 (#35705), we provide early runtime compatibility with JPA 4.0 and Hibernate ORM 8.0.0.Alpha1, effectively covering the JPA 3.2 usage model (plus a few configuration twists) when running against Hibernate 8.0 at runtime.

Following up on that basic support, we can add transactional support for EntityAgent management, similar to the existing support for Hibernate's StatelessSession in #7184:

The SmartFactoryBean#getObject(Class) mechanism can be used for dependency injection, along the lines of LocalSessionFactoryBean but following @PersistenceContext-style semantics for the EntityAgent proxy: delegating to a transactional EntityAgent if a transaction is active, performing a non-transactional operation otherwise.

The following additions are to be expected:

  • AbstractEntityManagerFactoryBean#getObject(Class) supporting a shared EntityAgent instance next to its shared EntityManager. This will require reflective loading of the EntityAgent type without a hard reference to it, plus conditional initialization of an untyped sharedEntityAgent field that can then be returned from getObject(Class) if the requested type is assignable to the reflectively loaded EntityAgent Class.
  • JpaVendorAdapter#getEntityAgentInterface() indicating a vendor-specific interface to use for the shared EntityAgent proxy, suggesting StatelessSession in HibernateJpaVendorAdapter.
  • Optional @PersistenceAgent injection support in PersistenceAnnotationBeanPostProcessor, along the lines of our existing @PersistenceContext support. Note that @PersistenceAgent is not available in the recently released JPA 4.0 M1 yet.

Complete EntityAgent support will require building against JPA 4.0 and might only be sensible to deliver in Spring Framework 8.0, then against a JPA 4.0 baseline (without any need for JPA 3.2 compatibility). That said, early reflective support is entirely possible in the meantime, covering all common use cases such as a shared @Autowired EntityAgent proxy with transactional management.