Created
April 27, 2017 20:45
-
-
Save sidneydemoraes/98babbaf8e8300a1ed938484f431eff2 to your computer and use it in GitHub Desktop.
TechTalk - Gradle + Spring Boot + Groovy - Repository com Query Nativa
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import br.com.smc.meurumo.domain.comercio.entity.Estabelecimento | |
import org.springframework.data.jpa.repository.Query | |
import org.springframework.data.repository.Repository | |
import org.springframework.data.repository.query.Param | |
/** | |
* Repositório de {@link Estabelecimento}. | |
*/ | |
interface EstabelecimentoRepository extends Repository<Estabelecimento, String> { | |
/** | |
* Busca a quantidade de {@link Estabelecimento} por lista de {@link br.com.smc.meurumo.domain.ibge.entity.SetorCensitario} | |
* e lista de {@link br.com.smc.meurumo.domain.comercio.entity.Cnae}. | |
* | |
* @param cnae | |
* @param listOfSectors | |
* @return | |
*/ | |
@Query(value = """ | |
SELECT COUNT(DISTINCT E.cnpj) | |
FROM estabelecimentos E | |
INNER JOIN estabelecimento_setor ES ON ES.cnpj = E.cnpj | |
INNER JOIN cnpj_cnae CC ON E.cnpj = CC.cnpj | |
INNER JOIN cnae CN ON CC.cod_cnae = CN.cod_cnae | |
WHERE CN.cod_cnae IN (:cnaes) | |
AND ES.setor_censitario IN (:setoresCensitarios) | |
""", | |
nativeQuery = true) | |
Integer obterContagemPorListaDeSetoresEListaDeCnaes( | |
@Param("cnaes") List<Integer> listaDeCnaes, | |
@Param("setoresCensitarios") List<Long> listaDeSetoresCensitarios); | |
/** | |
* Busca a lista de {@link Estabelecimento} por lista de {@link br.com.smc.meurumo.domain.ibge.entity.SetorCensitario} | |
* e lista de {@link br.com.smc.meurumo.domain.comercio.entity.Cnae}. | |
* | |
* @param cnae | |
* @param listOfSectors | |
* @return | |
*/ | |
@Query(value = """ | |
SELECT DISTINCT E | |
FROM Estabelecimento E | |
JOIN E.listaDeCnaes cnaes | |
WHERE | |
E.setorCensitario.id IN (:setoresCensitarios) | |
AND cnaes.id IN (:cnaes) | |
""") | |
List<Estabelecimento> findAllBySetorCensitarioIdInAndListaDeCnaesIdIn( | |
@Param("cnaes") List<Integer> listaDeCnaes, | |
@Param("setoresCensitarios") List<Long> listaDeSetoresCensitarios); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment