br.eti.danielcamargo.backend.hsnpts.core.business.ProgramaService.java Source code

Java tutorial

Introduction

Here is the source code for br.eti.danielcamargo.backend.hsnpts.core.business.ProgramaService.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.eti.danielcamargo.backend.hsnpts.core.business;

import br.eti.danielcamargo.backend.common.exceptions.BusinessException;
import br.eti.danielcamargo.backend.common.exceptions.ValidationOccurrence;
import br.eti.danielcamargo.backend.hsnpts.core.persistence.Aluno;
import br.eti.danielcamargo.backend.hsnpts.core.persistence.Programa;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Service;

/**
 *
 * @author Daniel
 */
@Service
public class ProgramaService {

    @PersistenceContext
    EntityManager em;

    public List<Programa> findByAlunoId(Long alunoId) throws BusinessException {
        StringBuilder hql = new StringBuilder();

        hql.append("SELECT ");
        hql.append(" p ");
        hql.append("FROM ");
        hql.append("  Programa p ");
        hql.append("  JOIN p.aluno a ");
        hql.append("WHERE ");
        hql.append("  a.id = :alunoId ");
        hql.append("ORDER BY ");
        hql.append("  p.dataInicio DESC");

        TypedQuery<Programa> query = em.createQuery(hql.toString(), Programa.class);
        query.setParameter("alunoId", alunoId);

        List<Programa> result = query.getResultList();

        return result;
    }

}