Skip to content

Commit 041da3b

Browse files
committed
Document how to configure a component that Hibernate depends upon
Closes gh-6333
1 parent bf618d5 commit 041da3b

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,6 +1881,27 @@ bean.
18811881

18821882

18831883

1884+
[[howto-configure-a-component-that-is-used-by-JPA]]
1885+
=== Configure a component that is used by JPA
1886+
If you want to configure a component that will be used by JPA then you need to ensure
1887+
that the component is initialized before JPA. Where the component is auto-configured
1888+
Spring Boot will take care of this for you. For example, when Flyway is auto-configured,
1889+
Hibernate is configured to depend upon Flyway so that the latter has a chance to
1890+
initialize the database before Hibernate tries to use it.
1891+
1892+
If you are configuring a component yourself, you can use an
1893+
`EntityManagerFactoryDependsOnPostProcessor` subclass as a convenient way of setting up
1894+
the necessary dependencies. For example, if you are using Hibernate Search with
1895+
Elasticsearch as its index manager then any `EntityManagerFactory` beans must be
1896+
configured to depend on the `elasticsearchClient` bean:
1897+
1898+
[source,java,indent=0]
1899+
----
1900+
include::{code-examples}/elasticsearch/HibernateSearchElasticsearchExample.java[tag=configuration]
1901+
----
1902+
1903+
1904+
18841905
[[howto-database-initialization]]
18851906
== Database initialization
18861907
An SQL database can be initialized in different ways depending on what your stack is. Or
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2012-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.elasticsearch;
18+
19+
import javax.persistence.EntityManagerFactory;
20+
21+
import org.springframework.boot.autoconfigure.data.jpa.EntityManagerFactoryDependsOnPostProcessor;
22+
import org.springframework.context.annotation.Configuration;
23+
24+
/**
25+
* Example configuration for configuring Hibernate to depend on Elasticsearch so that
26+
* Hibernate Search can use Elasticsearch as its index manager.
27+
*
28+
* @author Andy Wilkinson
29+
*/
30+
public class HibernateSearchElasticsearchExample {
31+
32+
// tag::configuration[]
33+
/**
34+
* {@link EntityManagerFactoryDependsOnPostProcessor} that ensures that
35+
* {@link EntityManagerFactory} beans depend on the {@code elasticsearchClient} bean.
36+
*/
37+
@Configuration
38+
static class ElasticsearchJpaDependencyConfiguration
39+
extends EntityManagerFactoryDependsOnPostProcessor {
40+
41+
ElasticsearchJpaDependencyConfiguration() {
42+
super("elasticsearchClient");
43+
}
44+
45+
}
46+
// end::configuration[]
47+
48+
}

0 commit comments

Comments
 (0)