Skip to content

wimdeblauwe/jpearl

Repository files navigation

JPearl - JPA Early Primary Key Library

Goal

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:

<dependency>
    <groupId>io.github.wimdeblauwe</groupId>
    <artifactId>jpearl-core</artifactId>
    <version>LATEST_VERSION_HERE</version>
</dependency>

Spring Boot compatibility

jpearl Spring Boot Minimum Java version

2.0.1

3.x

17

2.0.0

3.x

17

1.2.0

2.x

8

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 -DbasePackage=com.company.app -Dentity=MyEntity

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

To 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

Usage

  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)
            super(id);
        }
    }
    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 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;
        }
    }
    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.data.repository.CrudRepository;
    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;
        }
    
        @Override
        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()

      Tip

      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

Release is done via the Maven Release Plugin:

mvn release:prepare

and

mvn release:perform

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

Note

Before releasing, run export GPG_TTY=$(tty)