0

在 Hibernate 5.2.18中,可以将 hibernate 配置选项hibernate.cache.region.factory_class设置为类值。这允许我扩展类org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory并为我的集群应用程序实现所需的额外代码。然后可以将此实现的类作为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>

实现类:

public class MySingletonEhCacheRegionFactory extends
        SingletonEhCacheRegionFactory {
// Implementation
}

我现在正在尝试升级到 5.4.25 版,但是在 5.3.0 版中,SingletonEhCacheRegionFactory 被移动到了一个内部类并且代码被改变了。

最重要的是,hibernate.cache.region.factory_class配置现在需要类的缩短名称。这在Hibernate 5.4 文档中有说明。

例子:

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

所以我的问题是:

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

还是我应该扩展net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory课程?过去建议在此之上使用其他类(请参见此处),但它没有说明版本 5+。

对于 Hibernate 4,使用 org.hibernate.cache.ehcache.EhCacheRegionFactory 而不是 net.sf.ehcache.hibernate.EhCacheRegionFactory。

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

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

感谢您对上述内容的任何见解!

4

1 回答 1

1

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

如果您尝试扩展net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory您将面临一些问题,例如从 Hibernate 5.2 升级到 5.3 时 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,具有完整的类名:

#
# 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
于 2020-12-11T15:39:27.657 回答