Hibernate and object list

Asked

Viewed 298 times

1

I’m developing my first java application, desktop application with Netbeans and Hibernate.

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

from membros mb join mb.EstadoCivil"

My problem is that a list of objects is being returned, not members, and I can’t recover the data. How to return a Members list or convert Object to Members? Below 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;
}

1 answer

1

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 gonna be something, sort of, 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;
}

Browser other questions tagged

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