Hibernate 5.4.25 二级缓存和扩展 SingletonEhcacheRegionFactory class

Hibernate 5.4.25 second level cache and extending the SingletonEhcacheRegionFactory class

In Hibernate 5.2.18,可以将休眠配置选项 hibernate.cache.region.factory_class 设置为 class 值。这使我能够扩展 class org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory 并为我的集群应用程序实施所需的额外代码。然后可以将此实现的 class 引用为休眠设置中 hibernate.cache.region.factory_class 配置的配置值。

spring-config.xml中的示例:

<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">com.example.MySingletonEhCacheRegionFactory</prop>

已实施 class:

public class MySingletonEhCacheRegionFactory extends
        SingletonEhCacheRegionFactory {
// Implementation
}

我现在正在尝试升级到版本 5.4.25,但是 in version 5.3.0 SingletonEhCacheRegionFactory 已移至内部 class 并且代码已更改。

除此之外,hibernate.cache.region.factory_class 配置现在需要 shortened name for the class. This is stated in the Hibernate 5.4 documentation

示例:

<prop key="hibernate.cache.region.factory_class">ehcache-singleton</prop>       

所以我的问题是:

首先,是否仍然可以从新位置 org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory 扩展 SingletonEhcacheRegionFactory class 并将我的自定义实现作为缩短值添加到休眠配置中?

或者我应该扩展 net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory class 吗?它曾经被推荐使用另一个 class 而不是这个 (see here) 但它没有说明版本 5+.

For Hibernate 4, use org.hibernate.cache.ehcache.EhCacheRegionFactory instead of net.sf.ehcache.hibernate.EhCacheRegionFactory.

其次,我们可以使用缩短的名称在休眠配置中引用我们自己的 class 吗?

<prop key="hibernate.cache.region.factory_class">myEhcache-singleton</prop>     

如能深入了解上述内容,我们将不胜感激!

我认为您应该扩展 org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory class,但新的与 org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory 略有不同,因此您需要重写当前代码。

如果您尝试扩展 net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory,您将面临一些问题,例如

关于如何引用,正在实现org.hibernate.boot.registry.selector.StrategyRegistrationProvider接口,如:

package my.package.MyStrategyRegistrationProviderImpl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.boot.registry.selector.SimpleStrategyRegistrationImpl;
import org.hibernate.boot.registry.selector.StrategyRegistration;
import org.hibernate.boot.registry.selector.StrategyRegistrationProvider;
import org.hibernate.cache.spi.RegionFactory;

/**
 * Makes the 1 contained region factory implementations available to the Hibernate
 * {@link org.hibernate.boot.registry.selector.spi.StrategySelector} service.
 */
public class MyStrategyRegistrationProviderImpl implements StrategyRegistrationProvider {

    @Override
    @SuppressWarnings("unchecked")
    public Iterable<StrategyRegistration> getStrategyRegistrations() {
        final List<StrategyRegistration> strategyRegistrations = new ArrayList<StrategyRegistration>( 1 );

        strategyRegistrations.add(
                new SimpleStrategyRegistrationImpl(
                        RegionFactory.class,
                        CustomSingletonEhCacheRegionFactory.class,
                        "custom-ehcache-singleton",
                        CustomSingletonEhCacheRegionFactory.class.getName(),
                        CustomSingletonEhCacheRegionFactory.class.getSimpleName(),
                        // legacy impl class name
                        "org.hibernate.cache.SingletonEhCacheRegionFactory",
                        "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"
                )
        );

        return strategyRegistrations;
    }
}

并创建一个名为: META-INF/services/org.hibernate.boot.registry.selector.StrategyRegistrationProvider class 全名:

#
# Hibernate, Relational Persistence for Idiomatic Java
#
# License: GNU Lesser General Public License (LGPL), version 2.1 or later
# See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html
#
#
# Hibernate, Relational Persistence for Idiomatic Java
#
# License: GNU Lesser General Public License (LGPL), version 2.1 or later
# See the lgpl.txt file in the root directory or http://www.gnu.org/licenses/lgpl-2.1.html

my.package.MyStrategyRegistrationProviderImpl