SQL / JPQL Query

0

Friends, good afternoon.

I have a table in the database called Questao Inside the table I have two columns that are ID and Question . I have a filter that I can not specifically bring only code and asks because the method return is an object. I can only return the entire object.

How do I return only the code and question of the question table?

It's like I'm going to make one:

select codigo, pergunta from Questao where curso_codigo = ?1 and complexidade = ?2; 

Can anyone help me?

The method follows:

@SuppressWarnings("unchecked")
    public List<Questao> geraSimuladoPorFiltro(Long codigoCurso,
            Integer complexidade, Integer numeroDeQuestoes) {
        String query = "from Questao WHERE curso_codigo = ?1 AND complexidade = ?2";
                List<Questao> questoes = manager.createQuery(query)
                .setParameter(1, codigoCurso)
                .setParameter(2, complexidade)
                .setMaxResults(numeroDeQuestoes)
                .getResultList();
        for (Questao questao : questoes) {
            System.out.println(questao.getCodigo());
            System.out.println(questao.getPergunta());
        }
        return questoes;
    }
    
asked by anonymous 13.10.2015 / 21:06

1 answer

2

You can directly return a Map , just write the following query:

SELECT new Map(q.codigo, q.pergunta) FROM Questao q WHERE q.curso_codigo = ?1 AND q.complexidade = ?2 This query should return a Map object whose key will be the code and the value is Query.     
15.10.2015 / 15:13