Skip to content

Spring and Spring Boot

Martin Ledvinka edited this page Feb 11, 2025 · 3 revisions

This entry contains tips and useful information about using JOPA with Spring and Spring Boot.

Declarative Transactions

To use Spring declarative transactions (@Transactional annotation), you can use the JOPA-Spring-transaction project. Its GitHub page contains all the necessary information on how to set it up.

Nested JARs

If your project consists of multiple modules with JOPA entity classes being declared in a separate module from the one that contains the persistence unit declaration and setup, JOPA will not be able to discover the entity classes. This is caused by the structure of the JAR file produced by the Spring Boot Maven plugin. It contains nested JARs which are not by default readable by the default classpath scanner implemented in JOPA.

To work around this issue, use the custom BootAwareClasspathScanner provided by the JOPA Spring Boot Loader project. Add the following dependency to your project:

Maven dependency:

<dependency>
    <groupId>com.github.ledsoft</groupId>
    <artifactId>jopa-spring-boot-loader</artifactId>
</dependency>

And then register the classpath scanner with JOPA by adding it to the properties passed to Persistence to create an EntityManagerFactory.

Map<String, String> props = new HashMap<>();
// Other configuration, such as scanned package
props.put(JOPAPersistenceProperties.CLASSPATH_SCANNER_CLASS, BootAwareClasspathScanner.class.getName());

Note: The BootAwareClasspathScanner is not a direct part of JOPA because it would introduce a dependency on Spring Boot loader, which is something we currently do not want.