-
Notifications
You must be signed in to change notification settings - Fork 16
Spring and Spring Boot
This entry contains tips and useful information about using JOPA with Spring and Spring Boot.
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.
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.