使用EhCache进行Hibernate二级缓存

时间:2014-06-09 17:11:15

标签: java spring hibernate ehcache

我想在hibernate中配置二级缓存。我发现official documentation of EhCache并做到了。但我得到了例外:

Caused by: org.hibernate.cache.NoCacheRegionFactoryAvailableException: Second-level cache is used in the application, but property hibernate.cache.region.factory_class is not given; please either disable second level cache or set correct region factory using the hibernate.cache.region.factory_class setting and make sure the second level cache provider (hibernate-infinispan, e.g.) is available on the classpath.
at org.hibernate.cache.internal.NoCachingRegionFactory.buildEntityRegion(NoCachingRegionFactory.java:83) ~[NoCachingRegionFactory.class:4.3.3.Final]
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:364) ~[SessionFactoryImpl.class:4.3.3.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857) ~[Configuration.class:4.3.3.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) ~[EntityManagerFactoryBuilderImpl$4.class:4.3.4.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:843) ~[EntityManagerFactoryBuilderImpl$4.class:4.3.4.Final]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:397) ~[ClassLoaderServiceImpl.class:4.3.3.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:842) ~[EntityManagerFactoryBuilderImpl.class:4.3.4.Final]
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:150) ~[HibernatePersistenceProvider.class:4.3.4.Final]

这不是完整的异常消息,而是主要信息。

我的网络应用程序的配置:

private Properties hibernateProperties() {
    return new Properties() {{
        setProperty("hibernate.connection.characterEncoding", "UTF-8");
        setProperty("hibernate.connection.charSet", "UTF-8");
        setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect");
        setProperty("hibernate.show_sql", "true");
        setProperty("hibernate.format_sql", "true");
        setProperty("hibernate.generate_statistics", env.getProperty("hibernate.generate_statistics"));
        setProperty("hibernate.enable_lazy_load_no_trans", "true");

        setProperty("hibernate.cache.use_second_level_cache", "true");
        setProperty("hibernate.cache.use_query_cache", "true");
        setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
        /*setProperty("net.sf.ehcache.configurationResourceName", env.getProperty("net.sf.ehcache.configurationResourceName"));*/
    }};
}

@Bean
LocalSessionFactoryBean sessionFactory() {
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource(dataSource());
    sessionFactory.setPackagesToScan("com.smartestgift.*");
    sessionFactory.setHibernateProperties(hibernateProperties());
    return sessionFactory;
}

和EntityManager配置。我不认为这是主要问题,但我会告诉它代码:

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean();
    emf.setDataSource(dataSource());
    emf.setPackagesToScan("com.smartestgift.*");

    //let Hibernate know which database we're using.
    //note that this is vendor specific, not JPA
    Map opts = emf.getJpaPropertyMap();
    opts.put("hibernate.dialect", env.getProperty("hibernate.dialect"));

    HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();
    emf.setJpaVendorAdapter(va);

    return emf;
}

我无法理解问题所在。为什么冬眠不能看到那个属性?

更新:微米。 Denium - 谢谢!你是对的,我删除了实体管理器并重构了sessionFactory方法,现在它看起来像这样:

    @Bean
public SessionFactory sessionFactory() {
    LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean();
    sessionFactoryBean.setDataSource(dataSource());
    sessionFactoryBean.setPackagesToScan("com.smartestgift.*");

    Properties hibernateProperties = new Properties();
    hibernateProperties.put("hibernate.connection.characterEncoding", "UTF-8");
    hibernateProperties.put("hibernate.connection.charSet", "UTF-8");
    hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQL82Dialect");
    hibernateProperties.put("hibernate.show_sql", "true");
    hibernateProperties.put("hibernate.format_sql", "true");
    //hibernateProperties.put("hibernate.generate_statistics", env.getProperty("hibernate.generate_statistics"));
    hibernateProperties.put("hibernate.enable_lazy_load_no_trans", "true");

    // second level cache
    hibernateProperties.put("hibernate.cache.use_second_level_cache", "true");
    hibernateProperties.put("hibernate.cache.use_query_cache", "true");
    hibernateProperties.put("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory");
    //hibernateProperties.put("net.sf.ehcache.configurationResourceName", env.getProperty("net.sf.ehcache.configurationResourceName"));

    // testing
    hibernateProperties.put("hibernate.bytecode.use_reflection_optimizer", false);
    hibernateProperties.put("hibernate.check_nullability", false);
    hibernateProperties.put("hibernate.search.autoregister_listeners", false);
    sessionFactoryBean.setHibernateProperties(hibernateProperties);

    try {
        sessionFactoryBean.afterPropertiesSet();
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return sessionFactoryBean.getObject();
}

但是现在我遇到了新问题,我不知道如何实现OpenEntityManagerInViewInterceptor,因为在删除实体管理器之前我有下一个代码:

    OpenEntityManagerInViewInterceptor interceptor = new OpenEntityManagerInViewInterceptor();
    interceptor.setEntityManagerFactory(entityManagerFactory().getObject());

但是现在我还没有entityManagerFactory,我现在不知道如何制作这个拦截器......

0 个答案:

没有答案