Java - Hibernate criteria.setResultTransformer() 使用默认值初始化模型字段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19628759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java - Hibernate criteria.setResultTransformer() initializes model fields with default values
提问by Denis Rizov
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.
我是 Hibernate 的新手,我正在尝试从数据库中获取一些数据。我不想得到完整的数据,而是一个实体的投影。
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.
问题是,在 for 循环中,当我获得投影的 id 和名称时,它获得默认值 id=0 和 name=null 而不是 id=7 和 name="Name 8",它们是数据库中的原始实体。你知道是什么导致了这个问题吗?for 循环在最后一个代码中。
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();
}
采纳答案by JB Nizet
You probably simply forgot to assign aliases to your projections:
您可能只是忘记为您的投影分配别名:
Projections.projectionList()
.add(Projections.property("id"), "id")
.add(Projections.property("name"), "name")
回答by Thomas Champion
In addition and to respond to @Ram comment :
此外并回复@Ram 评论:
Projections.projectionList()
.add(Projections.property("id"), "id")
.add(Projections.property("name"), "name")
"id" and "name" are Java field name in your Student class, but in your database they are named "StudentId" and "Name", moreover hibernate generate SQL query with random alias to avoid name conflict, so in result set there are not "id" and "name" column. The second parameter in the above example force the alias name.
“id”和“name”是你Student类中的Java字段名,但在你的数据库中它们被命名为“StudentId”和“Name”,而且hibernate生成带有随机别名的SQL查询以避免名称冲突,因此在结果集中有不是“id”和“name”列。上面示例中的第二个参数强制使用别名。
Hibernate generate SQL like that :
Hibernate 生成这样的 SQL:
SELECT students0_.StudientId as StudientId12, students0_.Name as Name34, students0_.Grade as Grade11 FROM Students students0_
You can tell hibernate to display generated SQL query in console/log by setting hibernate.show_sql
to true in Hibernate config file hibernate.cfg.xml
.
您可以通过hibernate.show_sql
在 Hibernate 配置文件中设置为 true来告诉 hibernate 在控制台/日志中显示生成的 SQL 查询hibernate.cfg.xml
。