-1

Why, when you use an @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) annotation in @Entity, an error is displayed. How to fix this situation.

dependencies 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>

My entity:

@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;

  // ...

 }

I tried to add hibernate-core and hibernate-entitymanager, but unfortunately it did not help. The message is still displayed: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: 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.

And I need spring-boot-starter-data-jpa dependencies.

Grzegorz Kawalec
  • 325
  • 6
  • 19

2 Answers2

1

If you want to use second-level cache with hibernate, you need to add and configure a second-level cache provider (like ehcache or something like that). You can checkout this thread on How to enable second level cache in Hibernate.

abel90
  • 111
  • 2
  • 4
0

Please find below my pom file, I have similar setup like yours. Please try with below hibernate jars and see if it's working.

<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>

sayboras
  • 4,897
  • 2
  • 22
  • 40