有人可以帮我将以下原生查询转换为命名查询吗?
原生查询:
SELECT usr1.user_id, urr1.role_id, usr2.user_id, urr2.role_id, usr1.school_id, term.term_name, count(material.material_id) as "Total Book Count", fpc.FOLLETT_PENDING_COUNT as "Follett Pending Count", rrc.RESOLUTION_REQUIRED_COUNT as "Resolution Required Count" FROM va_school sch JOIN va_user_school_rel usr1 on sch.school_id=usr1.school_id JOIN va_user_role_rel urr1 on usr1.user_id=urr1.user_id and urr1.role_id=1001 JOIN va_user_school_rel usr2 on sch.school_id=usr2.school_id JOIN va_user_role_rel urr2 on usr2.user_id=urr2.user_id and urr2.role_id=1002 JOIN va_term term on term.school_id = usr1.school_id JOIN va_class course on course.term_id = term.term_id JOIN va_material material on material.class_id = course.class_id LEFT JOIN (SELECT VA_CLASS.TERM_ID as "TERM_ID", COUNT(*) as "FOLLETT_PENDING_COUNT" FROM VA_CLASS JOIN VA_MATERIAL ON VA_MATERIAL.CLASS_ID = VA_CLASS.CLASS_ID WHERE VA_CLASS.reference_flag = 'A' AND trunc(VA_MATERIAL.FOLLETT_STATUS) = 0 GROUP BY VA_CLASS.TERM_ID) fpc on term.term_id = fpc.term_id LEFT JOIN (SELECT VA_CLASS.TERM_ID as "TERM_ID", COUNT(*) as "RESOLUTION_REQUIRED_COUNT" FROM VA_CLASS JOIN VA_MATERIAL ON VA_MATERIAL.CLASS_ID = VA_CLASS.CLASS_ID WHERE VA_CLASS.reference_flag = 'A' AND trunc(VA_MATERIAL.FOLLETT_STATUS) = 1 GROUP BY VA_CLASS.TERM_ID) rrc on term.term_id = rrc.term_id WHERE course.reference_flag = 'A' GROUP BY usr1.user_id, urr1.role_id, usr2.user_id, urr2.role_id, usr1.school_id, term.term_name, fpc.FOLLETT_PENDING_COUNT, rrc.RESOLUTION_REQUIRED_COUNT ORDER BY usr1.school_id, term.term_name;
提前致谢。
答案 0 :(得分:0)
我建议将其移至存储过程并从JPA调用存储过程。
答案 1 :(得分:0)
根据您的对象 Object 模型,您应该考虑查询的目标。如果应该制定HQL查询,那么查看SQL语句在语义上是一个问题。
然而,即使是基于Hibernate的应用程序中的命名查询,也可以 - 通常 - 创建theta样式的查询。
HQL查询大部分时间与本机查询相同,但它使用了对象模型的关系。这些关系对方式有很大影响,可以定义查询。而不是从School
实体开始。它们可能从User
数据集开始。
SELECT ....
FROM User user1, User user2
JOIN User.school as school
...
WHERE school = user2.school
遵循to one
关系真的更容易,因此我通常会以某种方式重新构建我的查询,他们倾向于以这种方式执行。