This is a small library that simplifies usage of JPA/Hibernate. It adds:
- Better support for entity manager injection using mixin-like inheritance
- Better support for transactional code
- Support for persisting lambdas
- Junit 5 Persistence Tests
- Schema Generation
Just add WithSimplePersistenceUnit
:
import io.github.flbulgarelli.jpa.extras.simple.WithSimplePersistenceUnit;
public class UserRepository implements WithSimplePersistenceUnit {
public static UserRepository INSTANCE = new UserRepository();
public void addUser(User user) {
persist(user);
}
public User find(Long id) {
return find(User.class, id);
}
}
By using this mixin, you'll get access to an entityManager()
, plus some helper methods that act as shortcuts to
the corresponding EntityManager
:
persist
. See EntityManager#persistmerge
. See EntityManager#mergeremove
. See EntityManager#removefind
. See EntityManager#findcreateQuery
. See EntityManager#createQuery
In order to test code that has access to persistence context, just add the SimplePersistenceTest
mixin to your Junit 5 test classes:
import io.github.flbulgarelli.jpa.extras.test.SimplePersistenceTest;
public class ATest implements SimplePersistenceTest {
@Test
void aTestThatRequiresAPersistenceContext() {
// ....
}
}
By using this mixin, all your code is going to be run within a transaction. Also, you get access to an entityManager()
and
the EntityManagerOps
helpers:
import io.github.flbulgarelli.jpa.extras.test.SimplePersistenceTest;
public class ATest implements SimplePersistenceTest {
@Test
void aTestThatRequiresAPersistenceContext() {
User user = new User();
persist(user);
assertNotNull(user.getId());
}
}
JPA does not provide an easy way to generate the SQL schema. With this library, you can generate it by executing JpaSchemaExport
.
-
Usage:
JpaSchemaExport [...options]
-
Options:
-o,--output <arg>
: Output file. Defaults toschema.sql
.-f,--format
: If the output file should be formatted. Defaults tofalse
.-t,--target <arg>
: Target persistence unit. Defaults tosimple-persistence-unit
.-h,--help
: Print this message.
If you are using IntelliJ, you can add a Run Configuration with the following parameters:
- Main class:
io.github.flbulgarelli.jpa.extras.export.JpaSchemaExport
- Program arguments:
-o schema.sql -f
Modify Options
->Add Run Options
->Java
->Add provided dependencies with "provided" scope to classpath
jpa-extras is hosted in the Maven Central Repository. Simply add the following
dependency into your pom.xml
file:
<dependency>
<groupId>io.github.flbulgarelli</groupId>
<artifactId>jpa-extras</artifactId>
<version>1.0.0-rc1</version>
</dependency>
Also, snapshots of the master branch are deployed automatically with each successful commit. Instead of Maven Central, use the Sonatype snapshots repository at:
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
You can add the repository in your pom.xml
file:
<repositories>
<repository>
<id>sonatype-snapshots</id>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>