Document AOT workaround for @⁠PersistenceContext & @⁠PersistenceUnit in tests

Although @⁠PersistenceContext and @⁠PersistenceUnit are still not
supported in tests running in AOT mode, as of Spring Framework 7.0,
developers can inject an EntityManager or EntityManagerFactory into
tests using @⁠Autowired instead of @⁠PersistenceContext and
@⁠PersistenceUnit, respectively.

See commit 096303c477
See gh-33414
Closes gh-31442
This commit is contained in:
Sam Brannen
2025-11-05 14:30:41 +01:00
parent 798931eea8
commit 934394eb2a
4 changed files with 23 additions and 5 deletions
@@ -43,6 +43,16 @@ alternative, you can set the same property via the
xref:appendix.adoc#appendix-spring-properties[`SpringProperties`] mechanism.
====
[TIP]
====
JPA's `@PersistenceContext` and `@PersistenceUnit` annotations cannot be used to perform
dependency injection within test classes in AOT mode.
However, as of Spring Framework 7.0, you can inject an `EntityManager` or
`EntityManagerFactory` into tests using `@Autowired` instead of `@PersistenceContext` and
`@PersistenceUnit`, respectively.
====
[NOTE]
====
The `@ContextHierarchy` annotation is not supported in AOT mode.