Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

JPearl - JPA Early Primary Key Library


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.

Dependency coordinates

Maven coordinates to add the dependency to your project:


Maven plugin

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 -Dentity=MyEntity

This will generate the following classes/interfaces:






And the following tests:


If you want to shorten the command line typing, add the following to your ~/.m2/settings.xml file:


You can now run:

mvn jpearl:generate -Dentity=MyEntity

To avoid having to specify the base package each time, configure the plugin in your project:


The maven command now becomes as simple as:

mvn jpearl:generate -Dentity=MyEntity


  1. 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)
    1. A protected default constructor is required by JPA/Hibernate.

    2. A public constructor that will be used by the application itself.

  2. Create the entity. For example: User

    import io.github.wimdeblauwe.jpearl.AbstractEntity;
    import javax.persistence.Entity;
    @Entity //(1)
    public class User extends AbstractEntity<UserId> { //(2)
        private String name;
        protected User() { //(3)
        public User(UserId id, String name) { //(4)
   = name;
        public String getName() {
            return name;
    1. Annotate the class with @Entity so JPA will discover it.

    2. Extend from AbstractEntity and configure the used id class via generics.

    3. A protected default constructor is required by JPA/Hibernate.

    4. A public constructor that will be used by the application itself.

  3. Create a repository interface. For example: UserRepository

    import org.springframework.transaction.annotation.Transactional;
    @Transactional(readOnly = true) // (1)
    public interface UserRepository extends CrudRepository<User, UserId> { //(2)
    1. Mark transactions on the repo interface as read-only by default. If you later add finder methods to this UserRepository interface, 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 the readOnly).

    2. Use CrudRepository or PagingAndSortingRepository according to your needs. Use the entity and the entity id classes in the generics.

  4. Create a custom interface to extend the UserRepository interface with custom code. Example: UserRepositoryCustom:

    public interface UserRepositoryCustom { //(1)
        UserId nextId(); //(2)
    1. Make sure the name of the interface is the repository name, with Custom suffix.

    2. Add a method that returns the id type. Usually, this method is called nextId().

  5. Have the repository extend from the custom repository interface:

    @Transactional(readOnly = true)
    public interface UserRepository extends CrudRepository<User, UserId>, UserRepositoryCustom {
  6. 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;
        public UserId nextId() {
            return new UserId(generator.getNextUniqueId()); // (3)
    1. Be sure to name the class the repository name with Impl suffix

    2. Inject the unique id generator

    3. Generate a new unique id for each call to nextId()


    You 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();


Release is done via the Maven Release Plugin:

mvn release:prepare


mvn release:perform

Finally, push the local commits and the tag to remote.


Before releasing, run export GPG_TTY=$(tty)