Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Hibernate 5.3's ManagedBeanRegistry for dependency injection [SPR-16305] #20852

Closed
spring-issuemaster opened this Issue Dec 15, 2017 · 4 comments

Comments

Projects
None yet
2 participants
@spring-issuemaster
Copy link
Collaborator

commented Dec 15, 2017

Chris Cranford opened SPR-16305 and commented

Hibernate 5.3 introduces a new service contract called ManagedBeanRegistry.
https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/resource/beans/spi/ManagedBeanRegistry.java

This service contract would enable users of Spring to define Spring managed beans for various persistence objects such as Attribute Converters, Event Listeners, and Revision Listeners allowing Hibernate to lookup and use those beans as needed.


Issue Links:

  • #20850 Support for Hibernate ORM 5.3 ("is depended on by")
  • #21548 org.springframework.orm.hibernate5.SpringBeanContainer ignores the fallback bean instance producer
  • #21696 Support dependency injection in JPA AttributeConverter for EclipseLink
  • #21540 LocalSessionFactoryBean and HibernateTransactionManager for JPA EntityManagerFactory setup
  • #21494 Propagate read-only status to Hibernate Session through setDefaultReadOnly
  • #21368 Apply Hibernate Integrator through LocalSessionFactoryBean
  • #21581 Hibernate 5: restore ability to inject "cacheRegionFactory" of LocalSessionFactoryBean

Referenced from: commits 620e83c, c0d4cb5

2 votes, 4 watchers

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Dec 15, 2017

Chris Cranford commented

I would not mind supplying a PR to enable this feature for Spring; however, I wanted to first open a dialog about how Spring would like to see this implemented.

Currently, implementations can supply Hibernate with a service loader extension point, a ServiceContributor, which injects a StandardServiceInitiator implementation in Hibernate allowing us to locate and create various implementations and override default implementations of internal services. This initiator would simply supply Hibernate with spring's specific ManagedBeanRegistry implementation.

Ideally I would prefer to develop a solution that would work for all users who use Hibernate as a persistence layer, regardless of whether they're bootstrapping spring-orm with an EntityManagerFactory or whether they're using the native Hibernate 5 SessionFactory package classes. One idea would be for spring-orm to ship with a ServiceContributor service loader file allowing Hibernate 5.3 to bootstrap that automatically. For prior versions of Hibernate and non-Hibernate providers, this would be ignored.

Does this seem reasonable for you guys or is there another alternative you'd like for me to try and integrate this using?

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 4, 2018

Juergen Hoeller commented

For a start, Spring's LocalSessionFactoryBuilder provides Hibernate 5.3 BeanContainer support now, and LocalSessionFactoryBean automatically registers its containing BeanFactory as a BeanContainer when Hibernate 5.3 or higher is on the classpath. This goes nicely with #21540 and native Hibernate bootstrapping for JPA.

SpringBeanContainer is currently not public and therefore cannot be used independently, in particular not with Hibernate properties passed through standard JPA configuration via LocalContainerEntityManagerFactoryBean's "jpaProperties". Making it independently available is a bit tricky since the adapter needs access to the containing BeanFactory (which is an independent instance in Spring, not a static or JNDI-shared global setup).

As per #21540, an emerging strategy for specific Hibernate JPA support seems to be LocalSessionFactoryBean-driven bootstrapping as an alternative to LocalContainerEntityManagerFactoryBean for Hibernate 5.2+. There are currently a couple of differences in configuration style that prevent it from being a complete replacement though, so we'll have to reconsider our overall options for standard JPA setup in the 5.1 timeframe.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 5, 2018

Juergen Hoeller commented

SpringBeanContainer is public for custom JPA configuration purposes now, with XML and programmatic examples in its javadoc, e.g.:

LocalContainerEntityManagerFactoryBean emfb = ...
emfb.getJpaPropertyMap().put(AvailableSettings.BEAN_CONTAINER, new SpringBeanContainer(beanFactory));

Next to the above-mentioned support in native Hibernate LocalSessionFactoryBean and LocalSessionFactoryBuilder configuration.

@spring-issuemaster

This comment has been minimized.

Copy link
Collaborator Author

commented Jul 12, 2018

Chris Cranford commented

Juergen Hoeller, thanks for the effort to get this support in.  I'm sure users of both our communities will be thrilled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.