compile 'org.hibernate:hibernate-validator:4.0.0.GA'
compile 'org.hibernate:hibernate-entitymanager:3.6.6.Final'
compile 'org.hibernate.javax.persistence:hibernate-jpa-2.0-api:1.0.0.Final'
compile 'org.hibernate:hibernate-ehcache:3.3.1.GA'
compile 'org.springframework.data:spring-data-jpa:1.2.0.RELEASE'
public interface ServiceRepository extends CrudRepository<Service, Long>, JpaSpecificationExecutor<Service> {
@Cacheable("merchantServices")
@Query("select s from Service s JOIN s.statusList sas where s.status=?1 and s.priviligedUser.priviligedUserType IN (2,4) and (s.id IN (select st.id from Service st inner join st.tags tag where tag IN (?3)) or s.serviceType IN (?2)) and sas.active=true and sas.transactorType=?4 ORDER BY s.name")
List<Service> getAllMerchantServicesByStatusTypeAndTags(ServiceStatus status, List<ServiceType> type, List<String> tags, TransactorType transactor);
}
public List<Service> getAllServicesByStatusAndId(ServiceStatus status, List<Long> services, TransactorType transactor) {
return serviceRepository.getAllMerchantServicesByStatusAndServiceId(status, services, transactor);
}
它的灵感来自spring-data-jpa-examples/src/main/resources/caching-repository-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:p="http://www.springframework.org/schema/p"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"
default-autowire="byName">
<tx:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="merchantServices"/>
</set>
</property>
</bean>
</beans>
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.default_batch_fetch_size">16</property>
<property name="hibernate.max_fetch_depth">5</property>
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="hibernate.generate_statistics">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.c3p0.timeout">300</property>
</session-factory>
</hibernate-configuration>
看日志;每次请求时,我都会看到查询被执行。
17:50:28.997 [http-bio-8080-exec-10] INFO org.hibernate.stat.Statistics - HQL: select s from Service s JOIN s.statusList sas where s.status=?1 and s.priviligedUser.priviligedUserType IN (2,4) and s.id IN (?2) and sas.active=true and sas.transactorType=?3 ORDER BY s.name, time: 57ms, rows: 1
下次我请求时,我看到以下查询;
18:03:40.374 [http-bio-8080-exec-8] INFO org.hibernate.stat.Statistics - HQL: select s from Service s JOIN s.statusList sas where s.status=?1 and s.priviligedUser.priviligedUserType IN (2,4) and s.id IN (?2) and sas.active=true and sas.transactorType=?3 ORDER BY s.name, time: 47ms, rows: 1
中的jpa-context.xml丢失,添加了修复该问题的内容。最后的jpa-context.xml
是
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:p="http://www.springframework.org/schema/p"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd"
default-autowire="byName">
<tx:annotation-driven />
<cache:annotation-driven />
<bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">
<property name="caches">
<set>
<bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="merchantServices"/>
</set>
</property>
</bean>
</beans>
我还添加了resources/ehcache.xml
。
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">
<diskStore path="/home/prayag/cache_"/>
<defaultCache
eternal="false"
maxElementsInMemory="1000"
overflowToDisk="true"
diskPersistent="true"
timeToLiveSeconds="300"
/>
</ehcache>
在resources/hibernate.cfg.xml
中引用为
。
prayag@prayag:~/hacker_/draobkcalb$ sudo dmidecode -t cache
[sudo] password for prayag:
# dmidecode 2.11
SMBIOS 2.5 present.
Handle 0x000A, DMI type 7, 19 bytes
Cache Information
Socket Designation: Internal Cache
Configuration: Enabled, Not Socketed, Level 1
Operational Mode: Write Back
Location: Internal
Installed Size: 32 kB
Maximum Size: 32 kB
Supported SRAM Types:
Synchronous
Installed SRAM Type: Synchronous
Speed: Unknown
Error Correction Type: Unknown
System Type: Unknown
Associativity: Unknown
Handle 0x000B, DMI type 7, 19 bytes
Cache Information
Socket Designation: External Cache
Configuration: Enabled, Not Socketed, Level 2
Operational Mode: Write Back
Location: External
Installed Size: 2048 kB
Maximum Size: 2048 kB
Supported SRAM Types:
Synchronous
Installed SRAM Type: Synchronous
Speed: Unknown
Error Correction Type: Unknown
System Type: Unknown
Associativity: Unknown
我已经阅读了很多资料,但对于hibernate二级缓存,我无法消除一个疑问。 1) 比方说,我有一个返回1000条记录的查询。(例如,从年龄 2) 场景2。假设我执行查询(例如,从年龄
问题内容: 我正在尝试但未能成功在具有以下 依赖项的* Spring Data 和 Hibernate environmet中缓存查询: * 我的实体服务的Spring Data Repository(ServiceRepository)是 从中调用存储库的@Cacheable方法 我的缓存配置文件(jpa-context.xml)是 它的灵感来自spring-data-jpa-examples
我使用Spring Boot 1.4.1和spring-boot-starter-data-jpa 当查询我的自定义方法时,比如'find byname(String name)',它不是缓存。
本文向大家介绍hibernate查询缓存详细分析,包括了hibernate查询缓存详细分析的使用技巧和注意事项,需要的朋友参考一下 一、查询缓存配置 1、在hibernate.cfg.xml中加入查询缓存的策略, <propertyname="hibernate.cache.use_query_cache">true</property> 启用查询缓存的策略,默认是false。 二
我得到错误 我有JPA Hibernate配置和使用Eh缓存的查询缓存和二级缓存。 配置:PostgreSQL 9.6 JPA 2.1 Hibernate 5.2.3。最终的 我正在尝试使用SqlResultSetMapping[自定义结果集类]执行NativeQuery。当我禁用缓存时,一切正常。 但是,在启用缓存时出现上述错误。除了NativeQuery,缓存工作正常。 表架构: SQL本机查
问题内容: 我有一个关于Hibernate缓存机制的问题。我在文章中已经读到,在hibernate中执行本机SQLquery会使缓存的所有区域无效,因为hibernate不知道它将影响哪个特定实体。在这里,缓存的所有区域都是在讨论二级缓存或二级缓存(一级缓存,二级缓存)或仅二级缓存或仅一级缓存的各个区域吗? 问题答案: 该文章介绍了Hibernate的查询缓存是如何工作的,并原生查询的在现有的高速