Hibernate createCriteria list() return identical objects

1k Views Asked by At

can you help me with the code below. I'm querying Hibernate and the number of objects in the list returned corresponds to the number of rows in my table. But it seems that every object in the list is identical. Indeed the following line prints the same user while in the table is different.

 System.out.println("p"+mo.getUser());

here is my code

List<MyObject> lp = (List<MyObject>) sessionFactory.getCurrentSession().createCriteria(MyObject.class).list();

         for (MyObject mo: lp) {

              System.out.println("p"+mo.getUser());
          }

Do you know what could cause this behaviour? It's probably something stupid but I cannot find a solution. Thanks

MyObject code:

package soc.entities;

import java.math.BigDecimal;
import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;
/**
 * Part generated by hbm2java
 */

@Entity
@Table(name="Part")
public class MyObcejet {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;

    @Column
    private String user

    public Part() {
    }




    public Integer getId() {
        return this.id;
    }

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



    public String getUser() {
        return this.user;
    }

    public void setUser(String user) {
        this.user = user;
    }



}
2

There are 2 best solutions below

2
On

Please use the following line to avoid duplicate objects

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
0
On

I've managed to solve the issue. Actually MyObject instance variables didn't correspond to my table column names. Thanks!