/tkit-quarkus-jpa

Quarkus JPA layer

Primary LanguageJavaApache License 2.0Apache-2.0

tkit-quarkus-jpa

1000kit Quarkus JPA extension

License Maven Central GitHub Actions Status

RequestDataContext object move to the tkit-quarkus-context library

The version 1.0.0+ contains new model and DAO. For old version please use branch 0.7

Documentation

Example project with this extension is in the 1000kit JPA guides

This extension contains abstract classes for the JPA Entity and for DAO. The main class of this extension is AbstractDAO<T> class which implements basic CRUD operation.

Entity

We have these abstract classes for Entity

  • org.tkit.quarkus.jpa.models.TraceableEntity - base Entity abstract class which implements traceable fields creationUser,creationDate,modificationDate and modificationUser. The type of the ID field is String. The ID is generated when you create java instance with UUID.randomUUID().toString()

In the project you need to extend Entities from one of these abstract classes.

Business ID

For the business ID use corresponding pattern. The primary ID is GUID from the TraceableEntity.

@Entity
@Table(name = "BUSINESS_PROJECT")
public class BusinessProject extends TraceableEntity {

    @Generated(GenerationTime.INSERT)
    @Column(name = "bid", columnDefinition = "SERIAL")
    private Long bid;

    public Long getBid() { return bid; }
    public void setBid(Long bid) { this.bid = bid; }
}

DAO

The AbstractDAO<T> represent DAO pattern which implements CRUD operation.

@ApplicationScoped
public class UserDAO extends AbstractDAO<User> {
    
}

The operation create,delete,update and findById are implemented in the abstract class. In your DAO class you need to implement only the business logic.

Exception

All method of the AbstractDAO<T> class throws DAOException which is RuntimeException and has enumerated ErrorCode. These errors are defined in the AbstractDAO class.

The ConstraintException extends from the DAOException and is use for database constraints. For example the create or update operation can throw this exception.

PageQuery

The AbstractDAO class implements the PageQuery. With the method PagedQuery<T> createPageQuery(CriteriaQuery<T> query, Page page) could you create a PageQuery for you entity. The method getPageResult of the PageQuery return PageResult which contains:

  • stream - stream of entities.
  • totalElements - total elements in the database for your criteria.
  • number - the page number
  • size - size of the page
  • totalPages - total pages

Examaple method:

public PageResult<User> searchByCriteria(UserSearchCriteria criteria) {
    if (criteria == null) {
        return null;
    }
    CriteriaQuery<User> cq = criteriaQuery();
    Root<User> root = cq.from(User.class);
    List<Predicate> predicates = new ArrayList<>();
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();

    if (criteria.getName() != null && !criteria.getName().isEmpty()) {
        predicates.add(cb.like(root.get(User_.USERNAME), wildcard(criteria.getName())));
    }
    if (criteria.getEmail() != null && !criteria.getEmail().isEmpty()) {
        predicates.add(cb.like(root.get(User_.EMAIL), wildcard(criteria.getEmail())));
    }
    if (!predicates.isEmpty()) {
        cq.where(predicates.toArray(new Predicate[0]));
    }

    return createPageQuery(cq, Page.of(criteria.getPageNumber(), criteria.getPageSize())).getPageResult();
}

In version 2.8.0 default sorting by id attribute was added to avoid a problem with unpredictable data order for paging. There could be a situation where some rows are selected from DB more than once, and some rows were skipped.

Release

Create a release

mvn semver-release:release-create

Create a patch branch

mvn semver-release:patch-create -DpatchVersion=x.x.0