Unnecessary SELECT by Hibernate with second level cache

In the TestApplication class, I save the JPA object 3 times using a Hibernate-backed Data JPA repository configured with a L2 cache.

The first time you save the SQL INSERT, Hibernate is issued. In the second save, two SQL queries are issued by Hibernate: SELECT and UPDATE. Finally, on the third save, only HUBNATE will only return the SQL UPDATE.

Why do I have a SELECT query during the second save? The entity must already be in the second level cache after the first save, and the second save should behave like the third save.

Test.java:

@Entity
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Test {
    @Id
    @GeneratedValue
    private Long id;

    @Column(nullable = false)
    private String value;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }    
}

      

TestRepository.java:

public interface TestRepository extends JpaRepository<Test, Long> {
}

      

TestApplication.java:

@SpringBootApplication
public class TestApplication {
    private static final Logger LOGGER = LoggerFactory.getLogger(TestApplication.class);

    public static void main(String[] args) throws Exception {
        ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(TestApplication.class, args);
        TestRepository testRepository = configurableApplicationContext.getBean(TestRepository.class);

        Test test = new Test();
        test.setValue("test1");
        LOGGER.info("Save 1");
        testRepository.save(test);

        test.setValue("test2");
        LOGGER.info("Save 2");
        testRepository.save(test);

        test.setValue("test3");
        LOGGER.info("Save 3");
        testRepository.save(test);
    }
}

      

application.properties

logging.level.org.hibernate.SQL=DEBUG
spring.jpa.database: H2
spring.jpa.properties.hibernate.cache.region.factory_class=org.hibernate.cache.ehcache.EhCacheRegionFactory

      

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
    </dependency>
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
    </dependency>
</dependencies>

      

Execution log:

2015-07-07 10:53:33.865  INFO 3220 --- [           main] fr.test.TestApplication                  : Save 1
2015-07-07 10:53:33.950 DEBUG 3220 --- [           main] org.hibernate.SQL                        : insert into test (id, value) values (null, ?)
2015-07-07 10:53:34.002  INFO 3220 --- [           main] fr.test.TestApplication                  : Save 2
2015-07-07 10:53:34.029 DEBUG 3220 --- [           main] org.hibernate.SQL                        : select test0_.id as id1_0_0_, test0_.value as value2_0_0_ from test test0_ where test0_.id=?
2015-07-07 10:53:34.083 DEBUG 3220 --- [           main] org.hibernate.SQL                        : update test set value=? where id=?
2015-07-07 10:53:34.094  INFO 3220 --- [           main] fr.test.TestApplication                  : Save 3
2015-07-07 10:53:34.097 DEBUG 3220 --- [           main] org.hibernate.SQL                        : update test set value=? where id=?

      

+3


source to share





All Articles