嗨,这个问题已被问了很多次但仍然在我的应用程序中,二级缓存无法配置。我正在使用JPA + hibernate + Spring,我尝试了可能的配置但不成功的方法。
这些是为配置找到的链接
How to configure JPA 2.0 with Hibernate 3.5.2 to use EHCache as a Level 2 cache and query cache?
这是我的错误
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext-persistence.xml]: Invocation of init method failed; nested exception is 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.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:747)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:93)
at com.test.Test.main(Test.java:16)
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)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:364)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1859)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849)
这是我的persistence.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="ornament" transaction-type="RESOURCE_LOCAL">
<mapping-file>domain/orm/Category.orm.xml</mapping-file>
<!-- <mapping-file>META-INF/product.orm.xml</mapping-file> -->
<class>com.ornamentbazzar.common.catalog.entity.Category</class>
<class>com.ornamentbazzar.common.catalog.entity.CategoryMapper</class>
<class>com.ornamentbazzar.common.catalog.entity.CategoryMapperPK</class>
<class>com.ornamentbazzar.common.catalog.entity.CategoryAttribute</class>
<class>com.ornamentbazzar.common.catalog.entity.CategoryMapper</class>
<class>com.ornamentbazzar.common.assets.entity.StaticAsset</class>
<class>com.ornamentbazzar.common.assets.entity.StaticAssetDescription</class>
<class>com.ornamentbazzar.common.assets.entity.ImageStaticAsset</class>
<properties>
<property name="hibernate.transaction.flush_before_completion" value="false" />
<property name="hibernate.connection.autocommit" value="true" />
<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory"/>
<property name="hibernate.cache.provider_configuration" value="category-ehcache.xml" />
<property name="hibernate.generate_statistics" value="true" />
<property name="hibernate.id.new_generator_mappings" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
</properties>
</persistence-unit>
</persistence>
的applicationContext-的persistence.xml
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"
proxy-target-class="true" />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath:* META-INF/persistence.xml" />
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="ornament" />
<property name="packagesToScan" value="com.ornamentbazzar.*" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
<property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" />
</bean>
</property>
</bean>
这是我的pom.xml
<properties>
<spring.version>4.1.6.RELEASE</spring.version>
<spring.security.version>3.1.3.RELEASE</spring.security.version>
<hibernate.version>4.3.9.Final</hibernate.version>
</properties>
<dependencies>
<!-- Hibernate Libraries -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>${hibernate.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<dependencies>
用于缓存的类
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE, region = "categories")
public class Category implements Serializable {
private static final long serialVersionUID = 1L;
private static final Log LOG = LogFactory.getLog(Category.class);
@Id
@Column(name = "CATEGORY_ID")
@GeneratedValue(generator = "CategoryId")
@GenericGenerator(name = "CategoryId", strategy = "com.ornamentbazzar.common.util.CustomTableGenerator", parameters = {
@Parameter(name = "segment_value", value = "CategoryImpl"),
@Parameter(name = "entity_name", value = "com.ornamentbazzar.common.catalog.entity.Category") })
protected Long id;
@Column(name = "NAME", nullable = false)
@Index(name = "CATEGORY_NAME_INDEX", columnNames = { "NAME" })
protected String name;
@Column(name = "URL")
@Index(name = "CATEGORY_URL_INDEX", columnNames = { "URL" })
protected String url;
@Column(name = "URL_KEY")
@Index(name = "CATEGORY_URLKEY_INDEX", columnNames = { "URL_KEY" })
protected String urlKey;
@Column(name = "DESCRIPTION")
protected String description;
}
测试主要课程
public class Test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
new String[] { "applicationContext-persistence.xml" });
CategoryDao categoryDao = (CategoryDaoImpl) applicationContext
.getBean("CategoryDao");
Category category = categoryDao.findCategoryByName("jewellery");
System.out.println(category.getDescription());
}
}
目录
答案 0 :(得分:1)
这条线看起来很破碎
<property name="persistenceXmlLocation" value="classpath:* META-INF/persistence.xml" />
*
看起来很虚伪。或者看起来它应该是classpath*:
,它会合并你的类路径中找到的所有persistence.xml
,包括JAR,但我不确定你为什么要这样做。
即使您的问题使用persistence.xml
,该行也会引用persistent.xml
。所以首先要检查一下。