Hibernate and object list

0

I'm developing my first application in java, desktop application with Netbeans and hibernate.

I have tables membros and estadocivil . The member has a marital status. I mapped the classes using Hibernate and in the Members class it created the EstadoCivil attribute. My select in hibernate is

from membros mb join mb.EstadoCivil"

My problem is that a list of objects is being returned, not members, and I can not retrieve the data. How do I return a list of Members or convert the object to Members? Here is the code:

public List Select() {
    List lista = new ArrayList();
    try {
        this.sessao = Sessao.getSessao();
        transacao = sessao.beginTransaction();
        query = sessao.createQuery("from Membros");
        lista = query.list();
        sessao.close();
    } catch (HibernateException e) {
        JOptionPane.showMessageDialog(null, "Erro ao realizar consulta!\n" + 
            e.getMessage(), null, JOptionPane.ERROR_MESSAGE);
    }
    return lista;
}
    
asked by anonymous 21.06.2017 / 03:19

1 answer

0

Try to change the return of your method to the type of object you need, and NETBEANs itself will force you to cast.

There's going to be something more or less like this:

public Membro Select() {
    Membro lista = new ArrayList();
    try {
        this.sessao = Sessao.getSessao();
        transacao = sessao.beginTransaction();
        query = sessao.createQuery("from Membros");
        lista = (Membro) query.list();
        sessao.close();
    } catch (HibernateException e) {
        JOptionPane.showMessageDialog(null, "Erro ao realizar consulta!\n" + 
            e.getMessage(), null, JOptionPane.ERROR_MESSAGE);
    }
    return  lista;
}
    
21.06.2017 / 15:36