[英]JPA Hibernate SQL Query to NOT fetch data from one of the associated tables
[英]Fetch data from 4 tables join together using hibernate query
我有4张桌子:
教师,学生,课程及其公共联接表TeacherStudentCourse。
前三个表都与最后一个表具有一对多关系。 这是我的DB 数据库示例的屏幕截图。因此,例如,我应该能够通过提供教师ID和课程ID来获得学生的期望。
我以前在连接表上问过一个问题。 上一个问题而且我确实得到了答案。 我试图根据以前的代码进行改进
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
List<Object> list = session.createQuery("select s from Student s join s.teacherStudentCourses tsc where tsc.teacher = :teacher and tsc.course = :course")
.setParameter("teacher", teacher)
.setParameter("course", course)
.list();
session.getTransaction().commit();
session.close();
但经过无数次尝试,我似乎都无法使其正常工作。 也许查询无法以这种方式创建? 任何帮助将不胜感激!
[更新]
我得到的错误是:
org.hibernate.QueryException:无法解析属性:TeacherStudentCourses of:model.Student [从模型中选择s。学生加入s.teacherStudentCourses tsc,其中tsc.teacher =:teacher和tsc.course =:course]
在这里,表通过外键相互关联,因此所有子表(教师,课程,学生)在其POJO中都应具有父类型变量。
让这是Student.class中的“ TeacherStudentCource tsc”。 现在查询将是..
select s.studentId,s.name,s.gender,s.birthdate, from Student s inner join s.tsc b where b.teacherId=:tid and b.courceId=:cid;
老师ID = tid课程ID = cid
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.