简体   繁体   中英

Outputting data from db using jpa

Hi im trying to print all customers from db using jpa, but the output i get is in the following format : model.Customer@3d47d922, model.Customer@5f6f2271, model.Customer@6facbae5

Heres the dao method im using :

public List<Customer> getAllCustomers()
    {
        EntityManager em = null;
        List<Customer> resultList = new ArrayList<Customer>();
        try
        {
            em = JpaUtil.getFactory().createEntityManager();
            TypedQuery<Customer> query = em.createQuery("select p from Customer p", Customer.class);
            resultList = query.getResultList();

        }
        finally {

            JpaUtil.closeQuietly(em);
        }
        return resultList; 

Heres servlet where i try to output customers:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    CustomerDAO dao = new CustomerDAO();    

    response.getWriter().println(dao.getAllCustomers());

Heres my model class :

    @Entity
    public class Customer {

    @Id
    @SequenceGenerator(name = "my_seq", sequenceName = "seq1", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_seq")
    private Long id;


    private String first_name;


    private String surname;
    private String code;
    public enum Customertype{Private, Corporate};
    @Enumerated(EnumType.STRING)
    private Customertype customer_type;


    public Customer() {}


    public Long getId() {
        return id;
    }

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

    public String getFirstName() {
        return first_name;
    }

    public void setFirstName(String firstname) {
        first_name = firstname;
    }


    public String getSurname() {
        return surname;
    }


    public void setSurname(String surName) {
        surname = surName;
    }


    public Customertype getCustomertype() {
        return customer_type;
    }


    public void setCustomertype(Customertype customerType) {

        this.customer_type = customerType;
    }


    public String getCode() {
        return code;
    }


    public void setCode(String code) {
        this.code = code;
    }



}

You need to override the toString() method in your customer class. The toString() method is called whenever you try to print an object using println().

The default behavior of toString() in the Object class is to print the name of the class , an @ sign and a hascode for the object. That is what you are seeing.

For example

public String toString(){
    return this.getFirstName();
}

You need to override toString method in your class because it is going to give you clear information about the object in readable format that you can understand.

The merit about overriding toString :

Help the programmer for logging and debugging of Java program

Since toString is defined in java.lang.Object and does not give valuable information, so it is good practice to override it for subclasses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM