Querydsl is easier than Specifications and JPA Criterias.
--- /dev/null
+package de.spring.example.persistence.domain.specifications;
+
+import java.time.LocalDate;
+
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Predicate;
+import javax.persistence.criteria.Root;
+
+import org.springframework.data.jpa.domain.Specification;
+
+import de.spring.example.persistence.domain.Ad;
+
+public class AdSpectifications {
+
+ public static Specification<Ad> createdToday() {
+ return new Specification<Ad>() {
+
+ @Override
+ public Predicate toPredicate(Root<Ad> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
+ final LocalDate date = LocalDate.now();
+
+ return cb.equal(root.get("createdAt"), date);
+ }
+
+ };
+
+ }
+
+ public static Specification<Ad> mobileImage(String image) {
+ return new Specification<Ad>() {
+
+ @Override
+ public Predicate toPredicate(Root<Ad> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
+
+ return cb.equal(root.get("adMobileImage"), image);
+ }
+
+ };
+
+ }
+}
package de.spring.example.persistence.repository;
+import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
* Be careful with <code>@Transactional</code>. SimpleJpaRepository has annotated methods.
*
*/
-public interface AdRepository extends PagingAndSortingRepository<Ad, Long> {
+public interface AdRepository extends PagingAndSortingRepository<Ad, Long>, JpaSpecificationExecutor<Ad> {
// Named Native Query (using the native language of the store) It is not portable.
// See de.spring.persistence.example.domain.Ad
--- /dev/null
+package de.spring.example.services;
+
+import org.resthub.common.service.CrudService;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+
+import de.spring.example.persistence.domain.Ad;
+
+public interface AdService extends CrudService<Ad, Long> {
+
+ public Page<Ad> queryCriteriaExample(Pageable pageRequest);
+}
/**
* Using Querydsl. Giving some business logic to this service :)
+ *
+ * Querydsl: fluent interface done easy. There is no effort because it is already implemented.
+ *
+ * Criteria using Specifications requires some effort.
+ *
+ * See: de.spring.example.services.impl.AdServiceImpl
*/
@Override
public Page<AdDescription> queryDslExample(Pageable pageRequest) {
--- /dev/null
+package de.spring.example.services.impl;
+
+import static org.springframework.data.jpa.domain.Specifications.where;
+
+import javax.inject.Inject;
+
+import org.resthub.common.service.CrudServiceImpl;
+import org.springframework.data.domain.Page;
+import org.springframework.data.domain.Pageable;
+
+import de.spring.example.persistence.domain.Ad;
+import de.spring.example.persistence.domain.specifications.AdSpectifications;
+import de.spring.example.persistence.repository.AdRepository;
+import de.spring.example.services.AdService;
+
+public class AdServiceImpl
+ extends CrudServiceImpl<Ad, Long, AdRepository>
+ implements AdService {
+
+ @Override
+ @Inject
+ public void setRepository(AdRepository repository) {
+ this.repository = repository;
+ }
+
+ /**
+ * Criteria using Specifications.
+ *
+ * It is more complicated that when using Querydsl because I have to implement the
+ * Specifications. Querydsl is doing everything for me.
+ *
+ * See: de.spring.example.services.impl.AdDescriptionServiceImpl
+ */
+ @Override
+ public Page<Ad> queryCriteriaExample(Pageable pageRequest) {
+ return repository.findAll(
+ where(AdSpectifications.createdToday()).and(AdSpectifications.mobileImage("picasso")),
+ pageRequest);
+ }
+
+}