Document how to enable Hibernate 2nd level cache with JCache

Closes gh-14734
This commit is contained in:
Stephane Nicoll 2018-10-09 14:54:29 +02:00
parent acba77ab6d
commit 3b0f00dc20
3 changed files with 70 additions and 0 deletions

View File

@ -577,6 +577,11 @@
<artifactId>hibernate-core</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jcache</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>

View File

@ -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

View File

@ -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[]