How to display objects from a list? [duplicate]

0

I have the PessoaDAO class which has the following method:

public List<Pessoa> BuscarTodos() {

    List<Pessoa> list = null;

    EntityManager em = getEM();

    try {

        list = em.createQuery("select t from Pessoa t").getResultList();

    } catch (HibernateException e) {
        System.out.println(e.toString());

    } finally {
        em.close();
    }

    return list;

}

And in my Servlet, I have the following doGet:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    PessoaDAO<Pessoa> db = new PessoaDAO<Pessoa>();

    try {

    List<Pessoa> list = db.BuscarTodos();
    req.setAttribute("list", list);

    getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);

    System.out.println(req.getAttribute("list"));

    } catch (Exception e) {

        e.printStackTrace();
    }


}

Why when I give a sysout, it looks like this:

  

[model.Pessoa@7d5d4b78, model.Pessoa@1eb15301, model.Pessoa@15f8c5af]

And not the list itself?

If it helps, this is person class:

@Entity 
public class Pessoa {

    @Id
    @GeneratedValue
    @Column(name="ID")
    private int id;

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

    @Column(name="CPF")
    private String cpf;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCpf() {
        return cpf;
    }

    public void setCpf(String cpf) {
        this.cpf = cpf;
    }


}
    
asked by anonymous 07.10.2018 / 19:57

1 answer

-3

When you do this SELECT over hibernate, it creates an object to line from your database. These values that appear are the address that this object is allocated in its memory. When you print the object, it prints the address, not the data of that object. You can override the toString () method to print the data any way you want.

    
07.10.2018 / 20:56