Skip to content

Commit 83acd5b

Browse files
committed
Document JPA configuration best practices with AOT
Closes gh-30498
1 parent 67798a7 commit 83acd5b

File tree

1 file changed

+45
-0
lines changed
  • framework-docs/modules/ROOT/pages/core

1 file changed

+45
-0
lines changed

framework-docs/modules/ROOT/pages/core/aot.adoc

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,51 @@ Java::
318318
----
319319
======
320320

321+
[[aot.bestpractices.jpa]]
322+
=== JPA
323+
324+
The JPA persistence unit has to be known upfront for certain optmizations to apply. Consider the following basic example:
325+
326+
[tabs]
327+
======
328+
Java::
329+
+
330+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
331+
----
332+
@Bean
333+
LocalContainerEntityManagerFactoryBean customDBEntityManagerFactory(DataSource dataSource) {
334+
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
335+
factoryBean.setDataSource(dataSource);
336+
factoryBean.setPackagesToScan("com.example.app");
337+
return factoryBean;
338+
}
339+
----
340+
======
341+
342+
To make sure the scanning occurs ahead of time, a `PersistenceManagedTypes` bean must be declared and used by the
343+
factory bean definition, as shown by the following example:
344+
345+
[tabs]
346+
======
347+
Java::
348+
+
349+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
350+
----
351+
@Bean
352+
PersistenceManagedTypes persistenceManagedTypes(ResourceLoader resourceLoader) {
353+
return new PersistenceManagedTypesScanner(resourceLoader)
354+
.scan("com.example.app");
355+
}
356+
357+
@Bean
358+
LocalContainerEntityManagerFactoryBean customDBEntityManagerFactory(DataSource dataSource, PersistenceManagedTypes managedTypes) {
359+
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
360+
factoryBean.setDataSource(dataSource);
361+
factoryBean.setManagedTypes(managedTypes);
362+
return factoryBean;
363+
}
364+
----
365+
======
321366

322367
[[aot.hints]]
323368
== Runtime Hints

0 commit comments

Comments
 (0)