You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: spring-boot-project/spring-boot-docs/src/docs/asciidoc/howto.adoc
+18-36Lines changed: 18 additions & 36 deletions
Original file line number
Diff line number
Diff line change
@@ -1853,62 +1853,44 @@ Spring Boot auto-configuration switches off its entity manager in the presence o
1853
1853
1854
1854
1855
1855
[[howto-use-two-entity-managers]]
1856
-
=== Use Two EntityManagers
1857
-
Even if the default `EntityManagerFactory` works fine, you need to define a new one, otherwise the presence of the second bean of that type switches off the default.
1858
-
You can use the `EntityManagerBuilder` provided by Spring Boot to help you to create one.
1859
-
Alternatively, you can use the `LocalContainerEntityManagerFactoryBean` directly from Spring ORM, as shown in the following example:
1856
+
[[howto-use-multiple-entity-managers]]
1857
+
=== Using Multiple EntityManagerFactories
1858
+
If you need to use JPA against multiple data sources, you likely need one `EntityManagerFactory` per data source.
1859
+
The `LocalContainerEntityManagerFactoryBean` from Spring ORM allows you to configure an `EntityManagerFactory` for your needs.
1860
+
You can also reuse `JpaProperties` to bind settings for each `EntityManagerFactory`, as shown in the following example:
The example above creates an `EntityManagerFactory` using a `DataSource` bean named `firstDataSource`.
1868
+
It scans entities located in the same package as `Order`.
1869
+
It is possible to map additional JPA properties using the `app.first.jpa` namespace.
1870
+
1886
1871
NOTE: When you create a bean for `LocalContainerEntityManagerFactoryBean` yourself, any customization that was applied during the creation of the auto-configured `LocalContainerEntityManagerFactoryBean` is lost.
1887
1872
For example, in case of Hibernate, any properties under the `spring.jpa.hibernate` prefix will not be automatically applied to your `LocalContainerEntityManagerFactoryBean`.
1888
1873
If you were relying on these properties for configuring things like the naming strategy or the DDL mode, you will need to explicitly configure that when creating the `LocalContainerEntityManagerFactoryBean` bean.
1889
-
On the other hand, properties that get applied to the auto-configured `EntityManagerFactoryBuilder`, which are specified via `spring.jpa.properties`, will automatically be applied, provided you use the auto-configured `EntityManagerFactoryBuilder` to build the `LocalContainerEntityManagerFactoryBean` bean.
1890
1874
1891
-
The configuration above almost works on its own.
1892
-
To complete the picture, you need to configure `TransactionManagers` for the two `EntityManagers` as well.
1893
-
If you mark one of them as `@Primary`, it could be picked up by the default `JpaTransactionManager` in Spring Boot.
1894
-
The other would have to be explicitly injected into a new instance.
1875
+
You should provide a similar configuration for any additional data sources for which you need JPA access.
1876
+
To complete the picture, you need to configure a `JpaTransactionManager` for each `EntityManagerFactory` as well.
1895
1877
Alternatively, you might be able to use a JTA transaction manager that spans both.
1896
1878
1897
1879
If you use Spring Data, you need to configure `@EnableJpaRepositories` accordingly, as shown in the following example:
0 commit comments