Skip to content

Commit

Permalink
Document JPA configuration best practices with AOT
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll authored and mdeinum committed Jun 29, 2023
1 parent 71cce94 commit 063222f
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions framework-docs/modules/ROOT/pages/core/aot.adoc
Expand Up @@ -318,6 +318,51 @@ Java::
----
======

[[aot.bestpractices.jpa]]
=== JPA

The JPA persistence unit has to be known upfront for certain optmizations to apply. Consider the following basic example:

[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
@Bean
LocalContainerEntityManagerFactoryBean customDBEntityManagerFactory(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setPackagesToScan("com.example.app");
return factoryBean;
}
----
======

To make sure the scanning occurs ahead of time, a `PersistenceManagedTypes` bean must be declared and used by the
factory bean definition, as shown by the following example:

[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
@Bean
PersistenceManagedTypes persistenceManagedTypes(ResourceLoader resourceLoader) {
return new PersistenceManagedTypesScanner(resourceLoader)
.scan("com.example.app");
}
@Bean
LocalContainerEntityManagerFactoryBean customDBEntityManagerFactory(DataSource dataSource, PersistenceManagedTypes managedTypes) {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setManagedTypes(managedTypes);
return factoryBean;
}
----
======

[[aot.hints]]
== Runtime Hints
Expand Down

0 comments on commit 063222f

Please sign in to comment.