Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Gist com codigo complementar ao post descrevendo como trabalhar com Spring Data JPA e Criteria da JPA 2, via Specifications.
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.stereotype.Repository;
import br.com.yaw.springdatajpa.model.Mercadoria;
/**
* Contrato de persistência da entidade Mercadoria.
*
* @author <a href="mailto:eder@yaw.com.br">Eder Magalhães</a>
*/
@Repository
public interface MercadoriaRepository
extends JpaRepository<Mercadoria, Long>, JpaSpecificationExecutor<Mercadoria> {
}
/*********************************************************************************************/
import java.util.ArrayList;
import java.util.List;
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 br.com.yaw.springdatajpa.model.FiltrosPesquisaMercadoria;
import br.com.yaw.springdatajpa.model.Mercadoria;
import com.google.common.base.Strings;
/**
* Determina as <code>Specification<code>(s) da entidade <code>Mercadoria</code>.
*
* @author <a href="mailto:eder@yaw.com.br">Eder Magalhães</a>
*/
public final class MercadoriaSpecification {
private MercadoriaSpecification() {
}
public static Specification<Mercadoria> byNome(String name) {
return new Specification<Mercadoria>() {
@Override
public Predicate toPredicate(Root<Mercadoria> root,
CriteriaQuery<?> query, CriteriaBuilder builder) {
return builder.like(root.<String>get("nome"),
String.format("%s%", name.trim()));
}
};
}
public static Specification<Mercadoria> byCriterio(FiltrosPesquisaMercadoria filtros) {
return new Specification<Mercadoria>() {
@Override
public Predicate toPredicate(Root<Mercadoria> root,
CriteriaQuery<?> query, CriteriaBuilder builder) {
List<Predicate> predicates = new ArrayList<>();
if (!Strings.isNullOrEmpty(filtros.getDescricaoMercadoria())) {
predicates.add(builder.like(root.<String>get("descricao"),
filtros.getDescricaoMercadoria()));
}
if (!Strings.isNullOrEmpty(filtros.getNomeMercadoria())) {
predicates.add(builder.like(root.<String>get("nome"),
filtros.getNomeMercadoria()));
}
if (filtros.getPrecoDe() != null && filtros.getPrecoDe() > 0) {
predicates.add(builder.ge(root.<Double>get("preco"),
filtros.getPrecoDe()));
}
if (filtros.getPrecoAte() != null && filtros.getPrecoAte() > 0) {
predicates.add(builder.le(root.<Double>get("preco"),
filtros.getPrecoAte()));
}
return builder.and(predicates.toArray(new Predicate[]{}));
}
};
}
}
/*********************************************************************************************/
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import br.com.yaw.springdatajpa.model.FiltrosPesquisaMercadoria;
import br.com.yaw.springdatajpa.model.Mercadoria;
import br.com.yaw.springdatajpa.model.PesquisaMercadorias;
import br.com.yaw.springdatajpa.persistence.MercadoriaRepository;
import br.com.yaw.springdatajpa.persistence.MercadoriaSpecification;
import com.google.common.collect.Lists;
/**
* Controller das funcionalidade sobre a <code>Mercadoria</code>.
*
* @author <a href="mailto:eder@yaw.com.br">Eder Magalhães</a>
*/
@RestController
@RequestMapping(value="/")
public class MercadoriaController {
@Autowired
private MercadoriaRepository repository;
@RequestMapping(method = RequestMethod.GET)
public PesquisaMercadorias list(FiltrosPesquisaMercadoria filtros) {
Specification<Mercadoria> specification = MercadoriaSpecification.byCriterio(filtros);
long total = repository.count(specification);
List<Mercadoria> mercadorias =
Lists.newArrayList(repository.findAll(specification));
return new PesquisaMercadorias(total, mercadorias);
}
@RequestMapping(method = RequestMethod.GET, value="/{nome}")
public List<Mercadoria> listByNome(@PathVariable String nome) {
Specification<Mercadoria> specification = MercadoriaSpecification.byNome(nome);
return Lists.newArrayList(repository.findAll(specification));
}
}

hinotori commented Sep 2, 2016

ficou ótimo! eu colocaria o MercadoriaSpecification como um @component e chamaria ele diretamente

Valeu!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment