Đây là một mẫu Xuân dữ liệu, tuy nhiên tác phẩm của mình theo cùng một cách trong JPA
//HQL query
@Query("SELECT c,l,p,u FROM Course c, Lesson l, Progress p, User u "
+ "WHERE c.id=l.courseId AND l.id = p.lessonId AND p.userId = u.id AND u.id=:userId AND c.id=:courseId")
public List<Object[]> getLessonsWithProgress(@Param("userId") Integer userId, @Param("courseId")Integer courseId);
Sau đó, , Tôi gọi phương thức này và in kết quả:
List<Object[]> lst = courseRepository.getLessonsWithProgress(userId, courseId);
for (Object o[] : lst) {
Course c = (Course) o[0];
Lesson l = (Lesson) o[1];
Progress p = (Progress) o[2];
User u = (User) o[3];
//all the classes: Course, Lesson, Progress and User have the toString() overridden with the database ID;
System.out.printf("\nUser: %s \n Lesson: %s \n Progress: %s \n Course: %s",u,l,p,c);
}
Các @Test ra là ở đây:
User: com.cassio.dao.model.User[ id=1965 ]
Lesson: com.cassio.dao.model.Lesson[ id=109 ]
Progress: com.cassio.dao.model.Progress[ id=10652 ]
Course: com.cassio.dao.model.Course[ id=30 ]
Cheers
Nguồn
2014-05-26 20:58:00
@ John: Ngoài ra, bạn có thể gọi nhà xây dựng từ HQL, vì vậy bạn cũng có thể làm 'chọn new Foo (O1, O2, o3) ... 'và lấy Danh sách thay vì Danh sách
Ok, nhưng nếu tôi muốn sử dụng một JPA TypedQuery: myEntityManager.createQuery ("chọn o1, o2 ...",) –
AgostinoX
Điều gì sẽ xảy ra nếu bạn muốn sử dụng setFirstResult thực sự sẽ áp dụng cho nối của cả hai danh sách? tức là tôi muốn có thể trả về một danh sách được sắp xếp, đó là kết nối của kết quả cho hai thực thể được truy vấn. – Thomas