How do select in specific columns and continue to receive an entity list and not an Object array?

Asked

Viewed 198 times

2

Having the second 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;

        //getters e setters

    }

To make a Selct only in the columns name, surname and email I can do so:

SELECT u.nome, u.sobrenome, u.email FROM Usuario u

But in this case, I get back an Object[] list, where each array position equals one of the specified columns.

But is there any way to select these and return a User List?

2 answers

1

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.

  • 1

    really missed the new. I will edit my reply. + 1

  • 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.

  • 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.

0


An entity list will not be possible in your model, but what you can do is create a class with the attributes you need and provide a constructor with those attributes. Behold:

public class UsuarioAtributos {
  private String nome;
  private String sobreNome;
  private String email;

public UsuarioAtributos(String nome, String sobreNome, String email) {
    // definir os mapeamentos
}

And in your query you would define this way:

SELECT new meu.pacote.UsuarioAtributos(u.nome, u.sobreNome, u.email) FROM Usuario u

The return will be a list of User attributes and no longer an Object array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.