Completing the escapistabr’s response, it is possible to do, and it is practically what he has already said in his reply:
Assuming your User class is in the following package: br.com.meusistema.entities.
Ai you can do the jpql like this:
SELECT new br.com.meusistema.entidades.Usuario(u.nome, u.sobrenome, u.email) from Usuario u
Look at the new and on the road to class.
And it would be necessary to add a corresponding constructor in the User class:
public class Usuario {
private Integer id;
private Email email;
private String nome;
private String sobrenome;
private String senha;
private String usuario;
private List<Grupo> grupos;
//construtor padrão
public Usuario(){}
//construtor corresponde a busca
public Usuario(String nome, String sobrenome, String email){
this.nome = nome;
this.sobrenome = sobrenome;
this.email = email;
}
//getters e setters
}
Bottom line: you need a constructor with the attributes you are searching for in select, and add the new and the class path to JPQL, so you could receive a List as a query return, because for each table row an object will be instantiated using the constructor that corresponds to the query attributes, if you do not have a corresponding constructor, an exception will be made saying that there is no appropriate constructor.
really missed the new. I will edit my reply. + 1
– escapistabr
This. And just remember that it is not necessary to create a new class, with the constructor appropriating it is possible to query only with the User class.
– Resende
Exactly Resende, I took the test here and it worked. Thanks to you and to escapistabr for the answers, they helped me to do exactly what I wanted.
– R. Neves