Java - Hibernate criteria.setResultTransformer()使用默认值初始化模型字段

时间:2013-10-28 06:58:05

标签: java hibernate hibernate-criteria map-projections

我是Hibernate的新手,我正试图从数据库中获取一些数据。我不想获得完整的数据,只是实体的投影。

当我得到我的投影的id和名称时,在for循环中,它获得默认值id = 0和name = null而不是id = 7和name =“Name 8”数据库中原始实体的记录。你知道导致这个问题的原因吗? for循环在最后一个代码中。

这是学生实体

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

这是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;
    }
}

我正在执行的代码

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();
    }

2 个答案:

答案 0 :(得分:22)

您可能只是忘记为投影分配别名:

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

答案 1 :(得分:4)

此外并回复@Ram评论:

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

&#34; ID&#34;和&#34;名称&#34;是您的Student类中的Java字段名称,但在您的数据库中,它们的名称为&#34; StudentId&#34;而且&#34; Name&#34;此外,hibernate使用随机别名生成SQL查询以避免名称冲突,因此在结果集中没有&#34; id&#34;和&#34;名称&#34;柱。 上例中的第二个参数强制别名。

Hibernate生成SQL:

SELECT students0_.StudientId as StudientId12, students0_.Name as Name34, students0_.Grade as Grade11 FROM Students students0_

通过在Hibernate配置文件hibernate.show_sql中将hibernate.cfg.xml设置为true,可以告诉hibernate在控制台/日志中显示生成的SQL查询。