JPA + Hibernate + Native Query +结果集中的自定义dtos列表

时间:2010-03-05 14:04:29

标签: hibernate jpa ora-00923

下面我的原生查询工作正常oracle sqlplus。但是通过JPA本机查询,给出以下错误:

[错误] org.hibernate.util.JDBCExceptionReporter - ORA-00923:未找到FROM关键字

原生查询


SELECT sch.school_name, term.term_name, count(material.MATERIAL_ID), sip.SIP_COUNT, ... FROM VA_SCHOOL sch  JOIN va_user_school_rel usr1 on sch.school_id=usr1.school_id  

JOIN va_user ur ON usr1.USER_ID= ur.USER_ID  
JOIN va_user_role_rel urr1 on usr1.user_id=urr1.user_id  
JOIN va_role rle ON urr1.role_id= rle.role_id  
JOIN va_user_school_rel usr2 on sch.school_id=usr2.school_id   
JOIN va_user ur1 ON usr2.USER_ID= ur1.USER_ID  
JOIN va_user_role_rel urr2 on usr2.user_id=urr2.user_id  
JOIN va_role rle1 ON urr2.role_id= rle1.role_id  
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 TERM_ID, COUNT(*) as SIP_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.SCHOOL_STATUS) = 0  GROUP BY VA_CLASS.TERM_ID) sip on term.term_id = sip.term_id

WHERE course.reference_flag = 'A' AND rle.role_name='ROLE_1' AND rle1.role_name='ROLE_2'  

GROUP BY sch.school_name, term.term_name, sip.SIP_COUNT
ORDER BY sch.school_name, term.term_name

JPA原生查询:


SELECT new MyDTO(sch.school_name, term.term_name, count(material.MATERIAL_ID), sip.SIP_COUNT, ...) FROM VA_SCHOOL sch  
JOIN va_user_school_rel usr1 on sch.school_id=usr1.school_id  
JOIN va_user ur ON usr1.USER_ID= ur.USER_ID  
JOIN va_user_role_rel urr1 on usr1.user_id=urr1.user_id  
JOIN va_role rle ON urr1.role_id= rle.role_id  
JOIN va_user_school_rel usr2 on sch.school_id=usr2.school_id   
JOIN va_user ur1 ON usr2.USER_ID= ur1.USER_ID  
JOIN va_user_role_rel urr2 on usr2.user_id=urr2.user_id  
JOIN va_role rle1 ON urr2.role_id= rle1.role_id  
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 TERM_ID, COUNT(*) as SIP_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.SCHOOL_STATUS) = 0  GROUP BY VA_CLASS.TERM_ID) sip on term.term_id = sip.term_id  

WHERE course.reference_flag = 'A' AND rle.role_name=:p_roleName AND rle1.role_name=:p_roleName1  

GROUP BY sch.school_name, term.term_name, sip.SIP_COUNT 
ORDER BY sch.school_name, term.term_name

3 个答案:

答案 0 :(得分:0)

看看this question的答案。验证验证查询是否类似于SELECT 1 FROM DUAL;

答案 1 :(得分:0)

JPA原生查询不会将结果集转换为提供的DTO类型列表:(。我们需要明确地做。

谢谢,
斯里里

答案 2 :(得分:0)

JPA原生查询意味着,它是直接的SQL。 new MyDTO(...)不是SQL,所以sql解析器会拒绝它。 您必须提供映射在结果集上的实体,最容易通过使用@SqlResultSetMapping然后在查询中引用该映射来完成。 恕我直言,使用类似

的@NamedNativeQuery
@SqlResultSetMapping(
    name="yourResultSetMapping",
    entities = {
        @EntityResult(
            entityClass=YourResultSetEntity.class,
            fields = {
                @FieldResult(name = "<entity class field name", column = "result set column name"),
                 .
                 .
            }
        )
    }
 )

@NamedNativeQuery(
    name="getSchoolName",
    query = "<your very long query here",
    resultSetMapping = "yourResultMapping"
)

小心名称冲突。如果结果集中有多个列具有相同的名称,JPA将无法解析它们,并且随后会出现异常。在结果集中使用别名为每列赋予唯一的名称,并在@FieldResult的列部分中使用别名