通过Hibernate和二级缓存进行不必要的选择

通过Hibernate和二级缓存进行不必要的选择,hibernate,jpa,spring-data-jpa,ehcache,jpa-2.1,Hibernate,Jpa,Spring Data Jpa,Ehcache,Jpa 2.1,在TestApplication类中,我使用配置有二级缓存的Hibernate支持的Spring数据JPA存储库保存了一个JPA实体3次 在第一次保存时,Hibernate会发出SQL插入。 在第二次保存时,Hibernate发出两个SQL查询:一个SELECT查询和一个UPDATE查询。 最后,在第三次保存时,Hibernate只发布SQL更新 为什么在第二次保存期间有SELECT查询? 在第一次保存之后,实体应该已经在二级缓存中,第二次保存的行为应该与第三次保存的行为类似 Test.java

在TestApplication类中,我使用配置有二级缓存的Hibernate支持的Spring数据JPA存储库保存了一个JPA实体3次

在第一次保存时,Hibernate会发出SQL插入。 在第二次保存时,Hibernate发出两个SQL查询:一个SELECT查询和一个UPDATE查询。 最后,在第三次保存时,Hibernate只发布SQL更新

为什么在第二次保存期间有SELECT查询? 在第一次保存之后,实体应该已经在二级缓存中,第二次保存的行为应该与第三次保存的行为类似

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> {
}
应用程序属性

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