Taco Steemers

A personal blog.
☼ / ☾

Notes on DeltaSpike data access

Documentation

The official DeltaSpike documentation is here . Go to the data page to see all examples for data access. Take a look at the JPA page which describes the transaction management.

Pagination in DeltaSpike

Note that DeltaSpike pagination starts with page 1. This may be confusing if you are used to Spring Data for example, where the first page is page 0.

We can make a standard repository, and return a QueryResult object from our query method.

import org.apache.deltaspike.data.api.Query;
import org.apache.deltaspike.data.api.QueryParam;
import org.apache.deltaspike.data.api.QueryResult;
import org.apache.deltaspike.data.api.Repository;
import org.apache.deltaspike.jpa.api.transaction.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public abstract class MyDomainObjectRepository extends AbstractFullEntityRepository<MyDomainObject, Long> {

    @Query("SELECT o FROM my_domain_object o WHERE o.createdAt >= ?1 AND o.createdAt < ?2")
    public abstract QueryResult<MyDomainObject> findByStatusAndPeriod(final LocalDateTime from, final LocalDateTime to);
}

We can call this query method in the normal way, and we will get a QueryResult back. The query has not actually been executed yet at that point. Instead, we use the builder pattern to build up the query. We finalize with getResultList(), which executes the query.

List<MyDomainObject> results = myDomainObjectRepository.findByPeriod(from, to)
.withPageSize(pageSize).maxResults(pageSize).toPage(pageNumber - 1)
.getResultList()