Select only a few columns from Hibernate-mapped objects

Asked

Viewed 177 times

0

How can I select only a few fields of a related object ?

How it works (taking everything):

            List<Participante> participantes = this.entityManager.createQuery("SELECT a "
                + " FROM Participante a WHERE a.pessoa_id = :pessoa "
                + "AND a.motivo_id = :motivo")
                .setParameter("pessoa", pessoa).setParameter("motivo", motivo).getResultList();

As I have tried:

            List<Participante> participantes = this.entityManager.createQuery("SELECT "
                + "new Evento(a.evento_id.id, a.evento_id.nome, "
                + "new Pessoa(a.evento_id.criador_id.id, a.evento_id.criador_id.nome)) "
                + " FROM Participante a WHERE a.pessoa_id = :pessoa "
                + "AND a.motivo_id = :motivo")
                .setParameter("pessoa", pessoa).setParameter("motivo", motivo).getResultList();

With the constructor it does not present any error, without constructor it presents me error that could not map the object.

How do I turn my Participants into Objects (the 1 shown works):

    public List<Evento> pegaEventos(List<Participante> participantes) {
    List<Evento> eventos = new ArrayList<Evento>();
    try {
        for(Participante participante : participantes) {
            System.err.println("Evento ID: " + participante.getEvento_id().getId());
            eventos.add(participante.getEvento_id());
        }
        return eventos;
    } catch (Exception e) {

        System.err.println(junges.getFullStackTrace(e));
        return null;
    }
}

Modelo Evento:

    @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="nome")
private String nome;

@Column(name="descricao")
private String descricao;

@Column(name="preco")
private double preco;

@Column(name="idade")
private int idade;

@Column(name="minpessoas")
private int minpessoas;

@Column(name="maxpessoas")
private int maxpessoas;

@Column(name="dtinicio")
private Timestamp inicio;

@Column(name="dtfim")
private Timestamp fim;

@Column(name="criacao")
private Timestamp criacao;

@ManyToOne
@JoinColumn(name="categoria")
private Categoria categoria;

@Column(name="endereco")
private String endereco;

@ManyToOne
@JoinColumn(name="criador_id")
private Pessoa criador_id;

@Column(name="desativado")
private boolean desativado;

Modelo Pessoa:

    @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
private int id;

@Column(name="nome")
private String nome;

@Column(name="sobrenome")
private String sobrenome;

@Column(name="email")
private String email;

@Column(name="senha")
private String senha;

@Column(name="telefone")
private String telefone;

@Column(name="endereco")
private String endereco;

@ManyToOne
@JoinColumn(name="localizacao")
private Localizacao localizacao;

@Column(name="login")
private Timestamp login;

@Column(name="registro")
private Timestamp registro;

@Column(name="aniversario")
private Timestamp aniversario;

@Column(name="isAdm")
private int isAdm;

@Column(name="desativado")
private boolean desativado;

private double nota;
  • You said that with the class builder Evento and Pessoa no error. But it works? You can catch instantiated objects?

  • I have a Try Catch, which if any error occurs, it plays to displays the error in the console and returns null, if it is null, the entire application is canceled

  • Therefore, it does not arrive even in the return functionListEvents, which there has a sysout of the event id

  • You can’t put a breakpoint and check why it’s wrong within of the builder?

  • One thing that I find strange, is that in the other 2 (No constructor and everything) presents the SQL in the Console, with the constructor no. So, and the same builder, I use elsewhere.

  • I put breakpoint and nothing, no error for me, just does not display sql

  • Maybe you need to put the complete path of the class when instantiating with the new, take an example from me : hql1.append("SELECT new br.com.app.vo.DocumentoVO(...

  • It didn’t work, I put: new br.com.felipejunges.juntos.modelo.Evento( a.evento_id.id, a.evento_id.nome, ...)

  • You put in the new Pessoa( too? Although you can put that instantiation within constructor method, no need to call query.

  • I put it in the query and not in the constructor

  • Pass all attributes through the constructor, within the Event constructor you create Person.

  • Let me see if I understand. Don’t put new Person inside the query, put inside the constructor inside the Model ? Ex: Evento (int id, ..., new Pessoa(int id)) e dentro dele: this.pessoa = pessoa ? Or Evento(int id, ... pessoa) { this.pessoa = new Pessoa(...) } ?

  • That, you will pass the person parameter in the constructor (new br.com.pasta.Evento(id, nome, idPessoa, nomePessoa)) and within the constructor method in the class Evento will make the new Pessoa(idPessoa, nomePessoa).

  • It is not worked, it continues not even presenting SQL in the Console... My temporary solution is to take everything, and when returning the object, I use constructors to 'format' and pass only part of this information

Show 9 more comments
No answers

Browser other questions tagged

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