Hibernate 自定义查询加载器的东西位于org.hibernate.loader.custom.sql
(对于 Hibernate 3,似乎也是 Hibernate 4)。如果使用log4j,只需将此包设置为自己的类别即可打印日志(我建议您使用文件附加程序,因为如果使用控制台附加程序,之后的错误日志可能会与您感兴趣的内容重叠)。
<category name="org.hibernate.loader.custom.sql" additivity="false">
<priority value="trace" />
<appender-ref ref="fileAppender" />
</category>
假设您的根记录器显示文件中的每个错误,这就是您在查询加载时得到的错误输出:
17:27:18,348 TRACE SQLCustomQuery:85 - starting processing of sql query [SELECT equipment.*, det.*
FROM tdetectable_equipment equipment JOIN
tdetectable det
ON det.id = equipment.id_detectable
WHERE
equipment.id_detectable=det.id and det.active=1 and
equipment.id_warehouse_container = :_Id]
17:27:18,358 TRACE SQLCustomQuery:85 - starting processing of sql query [select line.* from tpacking_slip_line line join tpacking_slip slip on line.id_packing_slip = slip.id where line.id_detectable = :detectableId and line.id_related_packing_slip_line is null and slip.`type`= :slipType order by slip.date desc;]
17:27:18,359 TRACE SQLQueryReturnProcessor:387 - mapping alias [line] to entity-suffix [0_]
17:27:18,364 TRACE SQLCustomQuery:85 - starting processing of sql query [select res.* from tdetectable det join tpacking_slip_line line on det.id=line.id_detectable and line.id_related_packing_slip_line is null join tpacking_slip slip on line.id_packing_slip = slip.id and slip.`type`='OUT' join vreservation res on slip.id_reservation=res.id where det.id in ( :detIds ) group by(res.id) order by count(res.id) desc;]
17:27:18,365 TRACE SQLQueryReturnProcessor:387 - mapping alias [res] to entity-suffix [0_]
17:27:18,368 ERROR SessionFactoryImpl:424 - Error in named query: equipmentWarehouseQuery
org.hibernate.MappingException: Unknown collection role: com.mycompany.model.container.Container.detectables
at org.hibernate.impl.SessionFactoryImpl.getCollectionPersister(SessionFactoryImpl.java:701)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.addCollection(SQLQueryReturnProcessor.java:393)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processCollectionReturn(SQLQueryReturnProcessor.java:428)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.processReturn(SQLQueryReturnProcessor.java:358)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.process(SQLQueryReturnProcessor.java:171)
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:87)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:67)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:166)
at org.hibernate.impl.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:589)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:413)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:863)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:782)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:188)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:521)
该异常在加载时引发,但在执行代码时也可能出现查询错误。在这种情况下,会引发HibernateException或类似情况,您可以稍后对其进行检查。对于缺少冒号的情况,它实际上是AbstractQueryImpl类抛出的IllegalArgumentException :
java.lang.IllegalArgumentException: No positional parameters in query: SELECT equipment.*, det.*
FROM tdetectable_equipment equipment JOIN
tdetectable det
ON det.id = equipment.id_detectable
WHERE
equipment.id_detectable=det.id and det.active=1 and
equipment.id_container = _Id