Description
Koen Serneels opened SPR-13710 and commented
In Hibernate4 accessing the metadata was always possible on a live SessionFactory (SF) by for example using org.hibernate.cfg.Configuration#getClassMappings. The metadata is interesting as it can be used for modifying properties of entities, exporting SQL scripts and so forth.
However, these helper methods have now been removed as of Hibernate5 in favor of a new bootstrap mechanism: the APIs for building the
org.hibernate.SessionFactory are now backed by builders like: org.hibernate.boot.registry.BootstrapServiceRegistryBuilder,org.hibernate.boot.MetadataBuilder and org.hibernate.boot.SessionFactoryBuilder
Unfortunately Spring's org.springframework.orm.hibernate5.LocalSessionFactoryBean configures the SF using the legacy style by eventually using org.hibernate.cfg.Configuration#buildSessionFactory(). While the internal impl of
this method has been changed by Hibernate to use the builder style as well, there is no 'intermediate' access (or hook if you like) foreseen to access for example the metadata builder. This means that after the SF is returned there is no (clean) way to access the metadata.
So the ideal solution would be to to change org.springframework.orm.hibernate5.LocalSessionFactoryBean to make use of the builders and foreseeing additional hook methods to customize each of the builders before they are actually build. Another (maybe easier) solution might be to keep on using org.hibernate.cfg.Configuration#buildSessionFactory(), but allow to pass MetadaSources and/or BootstrapServiceRegistry to the constructor of Configuration (at this time Spring depends on the no-arg constructor of Configuration, which internally creates these objects and hence no reference is possible afterwards)
Affects: 4.2.3
Reference URL: https://www.mail-archive.com/hibernate-dev@lists.jboss.org/msg12865.html
Issue Links:
- LocalSessionFactoryBean (hibernate5) does not correctly pass custom ClassLoader to Hibernate [SPR-13879] #18452 LocalSessionFactoryBean (hibernate5) does not correctly pass custom ClassLoader to Hibernate
- Background initialization option for JPA EntityManagerFactory / Hibernate SessionFactory [SPR-13732] #18305 Background initialization option for JPA EntityManagerFactory / Hibernate SessionFactory
- Hibernate 5 LocalSessionFactoryBean should allow for dynamic mapping registration (like for Hibernate 4) [SPR-14815] #19381 Hibernate 5 LocalSessionFactoryBean should allow for dynamic mapping registration (like for Hibernate 4)
- Add setMultiTenantConnectionProvider to org.springframework.orm.hibernate5.LocalSessionFactoryBean [SPR-14072] #18644 Add setMultiTenantConnectionProvider to org.springframework.orm.hibernate5.LocalSessionFactoryBean