Java - Hibernate criteria.setResultTransformer()用默认值初始化模型字段 [英] Java - Hibernate criteria.setResultTransformer() initializes model fields with default values

查看:182
本文介绍了Java - Hibernate criteria.setResultTransformer()用默认值初始化模型字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



这是学生实体



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

@Column(name =Name,nullable = false,length = 50)
私有字符串名称;

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

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name =FacultyId,nullable = false)
私立教职员;

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

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

//所有字段的Setters和Getters
}

这是StudentModel

  public class StudentModel {
private int id;
私人字符串名称;

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(); (StudentModel student:students)

{
System.out.println(student.getId());
System.out.println(student.getName());
}

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


解决方案

别名到您的预测:

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


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

解决方案

You probably simply forgot to assign aliases to your projections:

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

这篇关于Java - Hibernate criteria.setResultTransformer()用默认值初始化模型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆