Java - Hibernate criteria.setResultTransformer() initializes model fields with default values

Denis Rizov picture Denis Rizov · Oct 28, 2013 · Viewed 25.7k times · Source

I am new to Hibernate and I am trying to get some data from the database. I don't want to get the full data but a projection of an entity.

The thing is that in the for-loop when I get the id and the name of my projection, it gets the default values id=0 and name=null instead of id=7 and name="Name 8" which are the records of the original entity in the database. Do you know what causes this problem? The for-loop is in the last code.

Here is the Student Entity

@Entity(name = "Students")
public class Student {
    @Id
    @GeneratedValue
    @Column(name = "StudentId")
    private int id;

    @Column(name = "Name", nullable = false, length = 50)
    private String name;

    @Column(name = "Grade")
    private Double grade = null;

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "FacultyId", nullable = false)
    private Faculty faculty;

    @ManyToMany(cascade = CascadeType.PERSIST)
    @JoinTable(
        joinColumns = @JoinColumn(name = "StudentId"),
        inverseJoinColumns = @JoinColumn(name = "CourseId"))
    private Collection<Course> courses;

    public Student() {
        this.courses = new HashSet<Course>();
    }

    // Setters and Getters for all fields
}

Here is the StudentModel

public class StudentModel {
    private  int id;
    private String name;

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

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

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

And the code I am executing

Session session = sessionFactory.openSession();
    session.beginTransaction();
    {
        Criteria criteria = session.createCriteria(Student.class);
        criteria.add(Restrictions.eq("name", "Name 8"))
                .setProjection(
                        Projections.projectionList()
                                .add(Projections.property("id"))
                                .add(Projections.property("name")))
                .setResultTransformer(
                        Transformers.aliasToBean(StudentModel.class));

        @SuppressWarnings("unchecked")
        List<StudentModel> students = criteria.list();

        for (StudentModel student : students) {
            System.out.println(student.getId());
            System.out.println(student.getName());
        }

        session.getTransaction().commit();
        session.close();
    }

Answer

JB Nizet picture JB Nizet · Oct 28, 2013

You probably simply forgot to assign aliases to your projections:

Projections.projectionList()
           .add(Projections.property("id"), "id")
           .add(Projections.property("name"), "name")