在@Entity中使用@Cache会出错

时间:2017-11-12 13:47:38

标签: java spring hibernate

为什么在@Entity中使用@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)注释时,会显示错误。如何解决这种情况。

依赖项pom.xml:

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.44</version>
    </dependency>
    <dependency>
        <groupId>org.liquibase</groupId>
        <artifactId>liquibase-core</artifactId>
        <version>3.5.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <version>1.5.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
        <version>1.5.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.5.8.RELEASE</version>
    </dependency>
</dependencies>

我的实体:

@Entity
@Table(name = "user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User implements Serializable {

  @Id
  @Column(name = "id")
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @Column(name = "active")
  private Boolean active;

  @Column(name = "email")
  private String email;

  @Column(name = "fist_name")
  private String fistName;

  @Column(name = "last_name")
  private String lastName;

  @Column(name = "created_at")
  private Date createdAt;

  // ...

 }

我尝试添加hibernate-core和hibernate-entitymanager,但不幸的是它没有帮助。消息仍然显示: org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / boot / autoconfigure / orm / jpa / HibernateJpaAutoConfiguration.class]中定义名称为'entityManagerFactory'的bean创建错误:init方法的调用失败;嵌套异常是org.hibernate.cache.NoCacheRegionFactoryAvailableException:在应用程序中使用了二级缓存,但是没有给出属性hibernate.cache.region.factory_class;请使用hibernate.cache.region.factory_class设置禁用二级缓存或设置正确的区域工厂,并确保类路径上有二级缓存提供程序(例如,hibernate-infinispan)。

我需要spring-boot-starter-data-jpa依赖项。

2 个答案:

答案 0 :(得分:1)

如果要在hibernate中使用二级缓存,则需要添加和配置二级缓存提供程序(如ehcache或类似的东西)。您可以在How to enable second level cache in Hibernate上查看此帖子。

答案 1 :(得分:0)

请在下面找到我的pom文件,我有类似你的设置。请尝试使用下面的休眠罐,看看它是否正常工作。

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.2.12.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>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>5.0.1.Final</version>
    </dependency>

</dependencies>