Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我们必须在Hibernate中使用@Cacheable和@Cache作为第二级缓存?_Java_Hibernate_Jpa_Caching_Ehcache - Fatal编程技术网

Java 为什么我们必须在Hibernate中使用@Cacheable和@Cache作为第二级缓存?

Java 为什么我们必须在Hibernate中使用@Cacheable和@Cache作为第二级缓存?,java,hibernate,jpa,caching,ehcache,Java,Hibernate,Jpa,Caching,Ehcache,我想知道为什么在Hibernate中必须使用2个注释来使用第二级缓存 我们宣布: @Cacheable @Cache 我们为什么不直接用选项声明@Cache呢 多谢各位 我想知道为什么我们必须使用2个注释来使用 Hibernate中的第二级缓存 您不需要同时使用这两个选项,但您可以。 @Cache是Hibernate缓存接口。 @Cacheable是JPA缓存接口。 @Cacheable仅当缓存元素(persistence.xml)设置为ENABLE\u SELECTIVE或DISABLE\u

我想知道为什么在Hibernate中必须使用2个注释来使用第二级缓存

我们宣布:

@Cacheable
@Cache
我们为什么不直接用选项声明@Cache呢

多谢各位

我想知道为什么我们必须使用2个注释来使用 Hibernate中的第二级缓存

您不需要同时使用这两个选项,但您可以。
@Cache
是Hibernate缓存接口。
@Cacheable
是JPA缓存接口。
@Cacheable
仅当缓存元素(
persistence.xml
)设置为
ENABLE\u SELECTIVE
DISABLE\u SELECTIVE
时才起作用

根据: 一些开发人员认为添加一个很好的约定。 标准@javax.persistence.Cacheable注释(虽然不是 Hibernate所需的)

我们为什么不直接用选项声明@Cache呢

这正是你应该做的。二级缓存的基本配置(我使用的是Spring Boot):

注1:上面使用的是Ehcache 2.x
注意2:默认情况下不会缓存集合,您需要使用
@Cache


奖金:
如果要检查统计信息,请执行以下操作:

//Add this property:
properties.put("hibernate.generate_statistics", "true");

//And somewhere in your code:
sessionFactory.getStatistics();

我相信
@Cacheable
是一个Spring注释,可以缓存任何一组数据,无论这些数据是否来自数据库。您能提供更多上下文吗?哪个文档/示例告诉您必须同时使用这两种注释?你能不能给出一个最小的代码示例,而不仅仅是两个注释?您使用的是哪个版本的Hibernate和JPA?
//Add this property:
properties.put("hibernate.generate_statistics", "true");

//And somewhere in your code:
sessionFactory.getStatistics();