Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-17043

Hibernate 5: restore ability to inject "cacheRegionFactory" of LocalSessionFactoryBean

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Complete
    • Affects Version/s: 4.3.18, 5.0.7
    • Fix Version/s: 5.1 RC1
    • Component/s: Data:ORM
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      Spring provided a convenience property for "cacheRegionFactory" in the Hibernate 3.x and 4.x packages. The current support for Hibernate 5.x no longer provides this flexibility. This is quite unfortunate, as there are some scenarios where customization of the underlying cache infrastructure is desired, and injection of various Spring-managed dependencies is the natural approach.

      Please note that the "hibernate.cache.region.factory_class" property is somewhat misleading: in addition to an implementation class, an actual object instance implementing org.hibernate.cache.spi.RegionFactory is equally valid: please see org.hibernate.cache.internal.RegionFactoryInitiator#initiateService.

      At the moment we are using Hibernate 5.1.x, and I have not verified that the approach described below is equally applicable to 5.0.x/5.1.x/5.2.x/5.3.x APIs.

      That said, the current workaround I am using is to subclass the hibernate5 LocalSessionFactoryBean as follows:

      public class MyLocalSessionFactoryBean extends LocalSessionFactoryBean {
      
          private RegionFactory regionFactory;
      
          @Required
          public void setRegionFactory(RegionFactory regionFactory) {
              this.regionFactory = regionFactory;
          }
      
          @Override
          protected SessionFactory buildSessionFactory(LocalSessionFactoryBuilder sfb) {
              sfb.getProperties().put(AvailableSettings.CACHE_REGION_FACTORY, regionFactory);
              return sfb.buildSessionFactory();
          }
      }

      (inspired by org.springframework.orm.hibernate5.LocalSessionFactoryBuilder#setMultiTenantConnectionProvider)

      If possible (across the range of Hibernate versions currently supported by Spring), it would be nice to have the "cacheRegionFactory" restored for 5.1. A backport to 5.0.x would be a bonus

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              plethora Taras Tielkes
              Last updater:
              Spring Issues Spring Issues
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                3 years, 31 weeks ago