Last active
June 27, 2022 20:47
-
-
Save regispires/952b0e2e91c303d3467ba63631b312a4 to your computer and use it in GitHub Desktop.
Exemplo de consulta SQL nativa via Spring Repository que retorna uma lista de tuplas (List<Tuple>)
This file contains hidden or 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 java.util.List; | |
import javax.persistence.Tuple; | |
import org.springframework.data.domain.Page; | |
import org.springframework.data.domain.Pageable; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.data.jpa.repository.Query; | |
import org.springframework.data.repository.query.Param; | |
import org.springframework.stereotype.Repository; | |
import quixada.npi.atividadecomplementar.model.EstadoEstudante; | |
import quixada.npi.atividadecomplementar.model.Estudante; | |
import quixada.npi.atividadecomplementar.model.Usuario; | |
@Repository | |
public interface EstudanteRepository extends JpaRepository<Estudante, Integer> { | |
public Estudante findByMatricula(String matricula); | |
public Estudante findByUsuario(Usuario usuario); | |
@Query("SELECT e FROM Estudante e JOIN e.usuario u JOIN e.curso c " | |
+ "WHERE (:nome IS NULL OR UPPER(u.nome) LIKE :nome) " | |
+ "AND (:matricula IS NULL OR e.matricula = :matricula) " | |
+ "AND (:cursoId IS NULL OR c.id = :cursoId) " | |
+ "AND (:estado IS NULL OR e.estado = :estado) " | |
+ "ORDER BY u.nome") | |
public Page<Estudante> findEstudantesFiltrados(@Param("nome") String nome, | |
@Param("matricula") String matricula, | |
@Param("cursoId") Integer cursoId, | |
@Param("estado") EstadoEstudante estado, | |
Pageable pageable); | |
@Query(value = "select c.id, c.nome, a.status, 'individual' as tipo, " + | |
" case " + | |
"when sum(a.horas_convertidas)>c.limite_categoria then c.limite_categoria " + | |
"when sum(a.horas_convertidas)<=c.limite_categoria then sum(a.horas_convertidas) " + | |
"end as computadas " + | |
"from solicitacao s " + | |
"join atividade a on (a.solicitacao_id=s.id) " + | |
"join categoria c on (c.id = a.id_categoria) " + | |
"where s.estudante_id= :estudanteId " + | |
"group by c.id, a.status " + | |
"union " + | |
"select c.id, c.nome, sc.status_solicitacao, 'coletiva' as tipo, " + | |
"case " + | |
"when sum(sc.horas_totais)>c.limite_categoria then c.limite_categoria " + | |
"when sum(sc.horas_totais)<=c.limite_categoria then sum(sc.horas_totais) " + | |
"end as computadas from solicitacao_coletiva sc " + | |
"join estudante_solicitacao_coletiva esc on (sc.id = esc.solicitacao_id) " + | |
"join categoria c on (c.id = sc.id_categoria) " + | |
"where esc.estudante_id= :estudanteId " + | |
"group by c.id, sc.status_solicitacao " + | |
"order by nome, tipo", nativeQuery = true) | |
List<Tuple> getDashboardInfos(@Param("estudanteId") Integer estudanteId); | |
@Query("select e from Estudante e join fetch e.usuario x " + | |
" where x.id = :idUser") | |
Estudante findByEstudanteByIdUser(@Param("idUser") Integer idUser); | |
@Query("select e from Estudante e where e.matricula in :matriculas") | |
public List<Estudante> findAllByMatricula(List<String> matriculas); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment