[英]Convert Hibernate Resultset to a pojo containing a list of pojos
我有一个类似于这样的pojo:
StudentResultPojo{
studentId;
studentName;
studentEmail;
List<CourseResultPojo> coursePojoList;
}
与CourseResultPojo看起来像这样:
CourseResultPojo{
name;
teacher;
time;
teacherPhone;
teacherEmail;
}
我有用于学生,课程,老师,电话,电子邮件等的Hibernate实体(数据库:Postgres)。
这是我的一小段代码:
String query = "SELECT
distinct student.id as \"studentId\",
student.name as \"studentName\",
student.email as \"studentEmail\"
FROM
sample.sample_Student student";
Query q = entityManager.unwrap(Session.class).
createSQLQuery(query).
setResultTransformer(Transformers.aliasToBean(StudentResultPojo.class));
return q.list();
我想将查询保留在NativeSQL中而不是JPQL中。 编辑:因为查询可以随时更改。 我想将查询存储在数据库中,并根据需要对其进行编辑。 如果在本机查询中,则测试会更容易。
我知道JPQL具有类似的功能,我们可以通过它的构造函数(如select new(Pojo.class.getName()....))将JPA查询的结果转换为pojo,但是我面临着同样的问题。
有了我拥有的东西,我将不得不遍历服务返回的StudentResultPojo列表,并且必须通过导航数据库或Java代码中的Hibernate关系来检索数据库或Java代码中的数据,从而为每个StudentResultPojo填充coursePojoList时间。
实体看起来像:
Student{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
private String name;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
private Set<Course> courses = new HashSet<>(0);
}
Course{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
private Long id;
private String name;
private String time;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "student_id", referencedColumnName = "pk")
@ForeignKey(name = "student_course")
private Student student;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "course")
private Set<Teacher> teacher;
}
......
问题:如果我有一个查询返回两行结果(不是JSON,仅是查询运行时返回的结果)
1, James, james.t@yahoo.com, Physics, Albert, 9.00, 7887897899, physics.teacher@yahoo.com
1, James, james.t@yahoo.com, English, Jackie, 10.00, 7887097899, english.teacher@yahoo.com
有没有一种方法可以将我的结果转换为带有CourseResultPojos列表的单个StudentResultPojo?
意味着我的StudentResultPojo将具有以下属性值:
1, James, james.t@yahoo.com for id, name and email respectively
并且该列表将具有两个CourseResultPojo对象,其中
1st Object values as Physics, Albert, 9.00, 7887897899, physics.teacher@yahoo.com for attributes name, teacher, time,teacherPhone,teacherEmail
2nd Object values as English, Jackie, 10.00, 7887097899, english.teacher@yahoo.com for attributes name,teacher,time,teacherPhone,teacherEmail
谢谢。
我想我会尽力帮助您解决以下问题: StudentResultPojo
和CourseResultPojo
您可以做什么来实现JSON Stris来为2个对象创建包装类(即StudentCoursePojo
):
Public class StudentCoursePojo {
private StudentResultPojo student;
private List<CourseResultPojo> courses;
// ...getters and setters
}
如果您希望将StudentCoursePojo
List
作为JSON字符串,猜猜是什么? 您还可以为其创建包装器:
Public class StudentCourseList {
private List<StudentCoursePojo > studentCourses;
// ...getters and setters
}
然后,您可以使用Jackson
或GSON
来将StudentCoursePojo
或StudentCourseList
序列化为JSON。
一些有用的教程:
http://www.mkyong.com/java/jackson-2-convert-java-object-to-from-json/
http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
希望这可以帮助。
编辑:
由于StudentResultPojo
已经包含CourseResultPojo
,因此您可以简单地构建此对象,然后为StudentResultPojo
List
创建包装器。 然后使用上述选项序列化为JSON。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.