GithubHelp home page GithubHelp logo

jpearl's Introduction

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>

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 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)
            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)

jpearl's People

Contributors

wimdeblauwe avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.