How to display objects from a list?

Asked

Viewed 140 times

0

I have the Personal 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();
    }


}

'Cause when I give a sysout, it shows up like this:

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

And not the list itself?

If it helps, this is class person:

@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;
    }


}

1 answer

-3

When you do this SELECT by Hibernate, it creates a row object from your database. These values that appear are the address that this object is allocated to in your memory. When you have the object printed, it printed the address and not the data of that object. You can override the toString() method to print the data however you want.

  • No, it is not memory address, this is wrong. I suggest you also read the questions that Linkei, because this information of your answer is not correct.

  • Yes. When printing an object from the list the toString method is called and it displays the "object name" that would be an identifier.

  • Not an identifier or memory address.

Browser other questions tagged

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