Skip to content

Commit

Permalink
Merge pull request #314 from devgateway/feature/jpa-query-cache-dgtkit3
Browse files Browse the repository at this point in the history
#311 Cache hibernate queries from JPA repositories dgtkit-3
  • Loading branch information
mpostelnicu authored May 19, 2021
2 parents b34621e + 38efd5a commit c6cfd12
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ spring.jpa.hibernate.jdbc.batch_versioned_data=true
spring.jpa.hibernate.bytecode.use_reflection_optimizer=true
spring.jpa.hibernate.bytecode.provider=javassist
spring.jpa.hibernate.cache.use_query_cache=true
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
spring.servlet.multipart.enabled = false

#enable modified flag for envers, to track field-level modifications
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.devgateway.toolkit.persistence.repository;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.persistence.QueryHint;

import org.springframework.data.jpa.repository.QueryHints;

/**
* @author Octavian Ciubotaru
*/
@Target({ ElementType.METHOD, ElementType.ANNOTATION_TYPE })
@Retention(RetentionPolicy.RUNTIME)
@QueryHints({@QueryHint(name = "org.hibernate.cacheable", value = "true")})
public @interface CacheHibernateQueryResult {
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,63 @@
package org.devgateway.toolkit.persistence.repository.norepository;

import org.devgateway.toolkit.persistence.repository.CacheHibernateQueryResult;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.lang.Nullable;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
* Created by Octavian on 01.07.2016.
*/
@NoRepositoryBean
public interface BaseJpaRepository<T, ID extends Serializable>
extends JpaRepository<T, ID>, JpaSpecificationExecutor<T> {

@Override
@CacheHibernateQueryResult
List<T> findAll();

@Override
@CacheHibernateQueryResult
List<T> findAll(Sort sort);

@Override
@CacheHibernateQueryResult
List<T> findAll(Specification<T> spec);

@Override
@CacheHibernateQueryResult
Page<T> findAll(Specification<T> spec, Pageable pageable);

@Override
@CacheHibernateQueryResult
Page<T> findAll(Pageable pageable);

@Override
@CacheHibernateQueryResult
List<T> findAll(Specification<T> spec, Sort sort);

@Override
@CacheHibernateQueryResult
Optional<T> findOne(@Nullable Specification<T> spec);

@Override
@CacheHibernateQueryResult
long count(Specification<T> spec);

@Override
@CacheHibernateQueryResult
long count();

@Override
@CacheHibernateQueryResult
Optional<T> findById(ID id);
}
4 changes: 4 additions & 0 deletions persistence/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

# Uncomment for Hibernate SQL statements and parameters
#logging.level.org.hibernate.SQL=DEBUG
#logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
4 changes: 4 additions & 0 deletions persistence/src/main/resources/ehcache.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,8 @@
<jcache:mbeans enable-management="true" enable-statistics="true"/>
</ehcache:cache>

<ehcache:cache alias="default-update-timestamps-region" uses-template="template" />

<ehcache:cache alias="default-query-results-region" uses-template="template" />

</ehcache:config>
3 changes: 1 addition & 2 deletions persistence/src/main/resources/liquibase-changelog.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,10 @@
<changeSet id="dgtkit-#291-autosave" author="mpostelnicu">
<preConditions onFail="CONTINUE">
<columnExists tableName="admin_settings" columnName="autosave_time"/>
<sqlCheck expectedResult="true">select autosave_time is null from admin_settings</sqlCheck>
</preConditions>

<update tableName="admin_settings">
<column name="autosave_time" value="10"/>
<column name="autosave_time" valueNumeric="10"/>
<where>autosave_time is null</where>
</update>
</changeSet>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

Expand All @@ -34,7 +33,6 @@ public boolean existsById(Long aLong) {
return false;
}


@Override
public List findAll() {
return null;
Expand Down Expand Up @@ -164,6 +162,4 @@ public long count(Example example) {
public boolean exists(Example example) {
return false;
}


}

0 comments on commit c6cfd12

Please sign in to comment.