1000kit Quarkus JPA extension
RequestDataContext
object move to thetkit-quarkus-context
library
The version 1.0.0+ contains new model and DAO. For old version please use branch 0.7
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.
We have these abstract classes for Entity
org.tkit.quarkus.jpa.models.TraceableEntity
- baseEntity
abstract class which implements traceable fieldscreationUser
,creationDate
,modificationDate
andmodificationUser
. The type of theID
field isString
. TheID
is generated when you create java instance withUUID.randomUUID().toString()
In the project you need to extend Entities
from one of these abstract classes.
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; }
}
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.
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.
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.
mvn semver-release:release-create
mvn semver-release:patch-create -DpatchVersion=x.x.0