diff --git a/spring-boot-project/spring-boot-docs/pom.xml b/spring-boot-project/spring-boot-docs/pom.xml index b21ea79db26..6c565a6913f 100644 --- a/spring-boot-project/spring-boot-docs/pom.xml +++ b/spring-boot-project/spring-boot-docs/pom.xml @@ -577,6 +577,11 @@ hibernate-core true + + org.hibernate + hibernate-jcache + true + org.hibernate.validator hibernate-validator diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc index c99ce0c1e5b..5b306ded72f 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc @@ -2003,6 +2003,27 @@ for more details. +[[howto-configure-hibernate-second-level-caching]] +=== Configure Hibernate Second-Level Caching +Hibernate {hibernate-documentation}#caching[second-level cache] can be configured for a +range of cache providers. Rather than configuring Hibernate to lookup the cache provider +again, it is better to provide the one that is available in the context whenever possible. + +If you're using JCache, this is pretty easy. First, make sure that +`org.hibernate:hibernate-jcache` is available on the classpath. Then, add a +`HibernatePropertiesCustomizer` bean as shown in the following example: + +[source,java,indent=0] +---- +include::{code-examples}/jpa/HibernateSecondLevelCacheExample.java[tag=configuration] +---- + +This customizer will configure Hibernate to use the same `CacheManager` as the one that +the application uses. It is also possible to use separate `CacheManager` instances, refer +to {hibernate-documentation}#caching-provider-jcache[the Hibernate user guide]. + + + [[howto-use-dependency-injection-hibernate-components]] === Use Dependency Injection in Hibernate Components By default, Spring Boot registers a `BeanContainer` implementation that uses the diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jpa/HibernateSecondLevelCacheExample.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jpa/HibernateSecondLevelCacheExample.java new file mode 100644 index 00000000000..af661f92e55 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/jpa/HibernateSecondLevelCacheExample.java @@ -0,0 +1,44 @@ +/* + * Copyright 2012-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.jpa; + +import org.hibernate.cache.jcache.ConfigSettings; + +import org.springframework.boot.autoconfigure.orm.jpa.HibernatePropertiesCustomizer; +import org.springframework.cache.jcache.JCacheCacheManager; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Example configuration of using JCache and Hibernate to enable second level caching. + * + * @author Stephane Nicoll + */ +// tag::configuration[] +@Configuration +public class HibernateSecondLevelCacheExample { + + @Bean + public HibernatePropertiesCustomizer hibernateSecondLevelCacheCustomizer( + JCacheCacheManager cacheManager) { + return (properties) -> properties.put(ConfigSettings.CACHE_MANAGER, + cacheManager.getCacheManager()); + + } + +} +// end::configuration[]