hibernate5.4.25二级缓存和扩展singletonehcacheregionfactory类

dsekswqp  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(411)

在Hibernate5.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 config现在需要类的缩写名称。这在Hibernate5.4文档中有说明。
例子:

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

所以我的问题是:
首先,是否仍有可能延长 SingletonEhcacheRegionFactory 从新位置初始化 org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory 并将我的自定义实现作为缩短值添加到hibernate配置中?
或者我应该延长 net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory 而不是上课?以前建议使用其他类(参见这里),但它没有提到版本5+。
对于hibernate 4,请使用org.hibernate.cache.ehcache.ehcacheregionfactory而不是net.sf.ehcache.hibernate.ehcacheregionfactory。
第二,我们可以在hibernate配置中使用缩写名引用我们自己的类吗?

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

任何洞察以上是感激的!

zvms9eto

zvms9eto1#

我认为你应该扩大范围 org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory 但新的和 org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory 所以你需要重写你当前的代码。
如果你试着 net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory 当从Hibernate5.2升级到5.3时,您将面临一些问题,比如SingletoneCacheRegionFactory不再可用
关于如何借鉴,正在实施中 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

相关问题