The goal of the project is to provide some convenient classes and interface to use JPA with early primary key generation.
It also encourages to use value objects as primary keys.
Maven coordinates to add the dependency to your project:
<dependency>
<groupId>io.github.wimdeblauwe</groupId>
<artifactId>jpearl-core</artifactId>
<version>LATEST_VERSION_HERE</version>
</dependency>To generate the entity and related classes, use the following direct invocation of the jpearl-maven-plugin:
mvn io.github.wimdeblauwe:jpearl-maven-plugin:VERSION_HERE:generate -DbasePackage=com.company.app -Dentity=MyEntityThis will generate the following classes/interfaces:
-
com.company.app.myentity.MyEntity -
com.company.app.myentity.MyEntityId -
com.company.app.myentity.MyEntityRepository -
com.company.app.myentity.MyEntityRepositoryCustom -
com.company.app.myentity.MyEntityRepositoryImpl
And the following tests:
-
com.company.app.myentity.MyEntityRepositoryTest
If you want to shorten the command line typing, add the following to your ~/.m2/settings.xml file:
<settings>
<pluginGroups>
<pluginGroup>io.github.wimdeblauwe</pluginGroup>
</pluginGroups>
</settings>You can now run:
mvn jpearl:generate -DbasePackage=com.company.app -Dentity=MyEntityTo avoid having to specify the base package each time, configure the plugin in your project:
<project>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>io.github.wimdeblauwe</groupId>
<artifactId>jpearl-maven-plugin</artifactId>
<version>${jpearl.version}</version>
<configuration>
<basePackage>com.company.app</basePackage>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>The maven command now becomes as simple as:
mvn jpearl:generate -Dentity=MyEntity-
Create a primary value object. For example:
UserId:import io.github.wimdeblauwe.jpearl.AbstractEntityId; import java.util.UUID; public class UserId extends AbstractEntityId<UUID> { protected UserId() { //(1) } public UserId(UUID id) { //(2) super(id); } }
-
A
protecteddefault constructor is required by JPA/Hibernate. -
A
publicconstructor that will be used by the application itself.
-
-
Create the entity. For example:
Userimport io.github.wimdeblauwe.jpearl.AbstractEntity; import jakarta.persistence.Entity; @Entity //(1) public class User extends AbstractEntity<UserId> { //(2) private String name; protected User() { //(3) } public User(UserId id, String name) { //(4) super(id); this.name = name; } public String getName() { return name; } }
-
Annotate the class with
@Entityso JPA will discover it. -
Extend from
AbstractEntityand configure the used id class via generics. -
A
protecteddefault constructor is required by JPA/Hibernate. -
A
publicconstructor that will be used by the application itself.
-
-
Create a repository interface. For example:
UserRepositoryimport org.springframework.data.repository.CrudRepository; import org.springframework.transaction.annotation.Transactional; @Transactional(readOnly = true) // (1) public interface UserRepository extends CrudRepository<User, UserId> { //(2) }
-
Mark transactions on the repo interface as read-only by default. If you later add finder methods to this
UserRepositoryinterface, then the transactions of each method will be read-only which is best for finders. If there is a modifying query, be sure to individually annotate that method with@Transactional(without thereadOnly). -
Use
CrudRepositoryorPagingAndSortingRepositoryaccording to your needs. Use the entity and the entity id classes in the generics.
-
-
Create a custom interface to extend the
UserRepositoryinterface with custom code. Example:UserRepositoryCustom:public interface UserRepositoryCustom { //(1) UserId nextId(); //(2) }
-
Make sure the name of the interface is the repository name, with
Customsuffix. -
Add a method that returns the id type. Usually, this method is called
nextId().
-
-
Have the repository extend from the custom repository interface:
@Transactional(readOnly = true) public interface UserRepository extends CrudRepository<User, UserId>, UserRepositoryCustom { }
-
Create a class to implement the custom interface. Example:
UserRepositoryImpl:import io.github.wimdeblauwe.jpearl.UniqueIdGenerator; import java.util.UUID; public class UserRepositoryImpl implements UserRepositoryCustom { //(1) private final UniqueIdGenerator<UUID> generator; public UserRepositoryImpl(UniqueIdGenerator<UUID> generator) { // (2) this.generator = generator; } @Override public UserId nextId() { return new UserId(generator.getNextUniqueId()); // (3) } }
-
Be sure to name the class the repository name with
Implsuffix -
Inject the unique id generator
-
Generate a new unique id for each call to
nextId()TipYou usually have a repository per aggregate root. Entities within that root will not have their own repository, but there will be an extra method on the custom interface to generate primary keys. E.g.:
public interface PostRepositoryCustom { PostId nextId(); PostCommentId nextCommentId(); }
-