Tas*_*kos 42
IIRC,你可以做一个SELECT o1, o2, o3 FROM EntityA o1, EntityB o2, EntityC o3 WHERE ....,结果将是a List<Object[3]>,其中数组内容将包含o1,o2,o3值.
Cas*_*rin 24
这是一个Spring Data示例,但它在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);
Run Code Online (Sandbox Code Playgroud)
然后,我调用此方法并打印结果:
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);
}
Run Code Online (Sandbox Code Playgroud)
输出@Test在这里:
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 ]
Run Code Online (Sandbox Code Playgroud)
干杯
| 归档时间: |
|
| 查看次数: |
62720 次 |
| 最近记录: |