繁体   English   中英

Hibernate 5.4.25 二级缓存和扩展 SingletonEhcacheRegionFactory class

[英]Hibernate 5.4.25 second level cache and extending the SingletonEhcacheRegionFactory class

In Hibernate 5.2.18 , it was possible to set the hibernate configuration option hibernate.cache.region.factory_class to a class value. 这允许我扩展 class org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory并为我的集群应用程序实现所需的额外代码。 然后可以将这个实现的 class 引用为 hibernate 设置中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 版,但是在 5.3.0 版中,SingletonEhCacheRegionFactory 被移动到内部 class 并且代码已更改。

最重要的是, hibernate.cache.region.factory_class配置现在需要 class 的缩写名称 Hibernate 5.4 文档中对此进行了说明。

例子:

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

所以我的问题是:

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

或者我应该扩展net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory class 吗? 过去建议使用其他 class ( 请参阅此处),但它没有说明版本 5+。

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

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

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

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

I think you should extent the org.hibernate.cache.ehcache.internal.SingletonEhcacheRegionFactory class, but the new one is slightly different from the org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory so you will need to rewrite your current code.

如果您尝试扩展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的文件,其完整的 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM