【问题标题】:Convert Hibernate Resultset to a pojo containing a list of pojos将 Hibernate 结果集转换为包含 pojo 列表的 pojo
【发布时间】:2016-09-15 15:42:24
【问题描述】:

我有一个类似这样的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。 编辑:因为查询可以随时更改。我想将查询存储在数据库中并根据需要进行编辑。如果在 Native 查询中测试会更容易。

我知道 JPQL 有一个类似的功能,我们可以通过它的构造函数将 JPA 查询的结果转换为 pojo,比如 select new (Pojo.class.getName()....) 但这和我面临的问题相同。

使用我所拥有的,我将不得不遍历我的服务返回的 StudentResultPojo 列表,并且我必须通过从数据库中检索数据或在 Java 代码中通过导航它们的 Hibernate 关系来为每个 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;
}
......

问题:如果我有一个以 2 行返回结果的查询(不是 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

谢谢。

【问题讨论】:

标签: java spring hibernate resulttransformer


【解决方案1】:

我想我会尽力为您提供帮助:StudentResultPojoCourseResultPojo

你可以做些什么来实现 JSON Stris 来为 2 个对象创建一个包装类(即StudentCoursePojo):

Public class StudentCoursePojo {
   private StudentResultPojo student;
   private List<CourseResultPojo> courses;
   // ...getters and setters
} 

如果你想要一个 ListStudentCoursePojo 作为 JSON 字符串,你猜怎么着?您还可以为其创建一个包装器:

    Public class StudentCourseList {
        private List<StudentCoursePojo > studentCourses;
         // ...getters and setters
    } 

然后您可以使用JacksonGSON 将您的StudentCoursePojoStudentCourseList 序列化为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,您可以简单地构建此对象,然后为ListStudentResultPojo 创建一个包装器。然后使用上述选项序列化为 JSON。

【讨论】:

  • 我编辑了我的问题。我可以看到我想将我的 pojo 转换为 JSON 的样子。对于那个很抱歉。那不是我想要的。我想将我的查询结果与 CourseResultPojos 列表一起转换为 StudentResultPojo。我还包括了坚持使用 Native 而不是 JPA 的原因。感谢您的帮助。
猜你喜欢
  • 2014-11-17
  • 2020-03-06
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-25
  • 1970-01-01
相关资源
最近更新 更多