java – 如何让hibernate打印出命名查询有什么问题?

前端之家收集整理的这篇文章主要介绍了java – 如何让hibernate打印出命名查询有什么问题?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的 Spring / Hibernate / JPA应用程序中,我使用了很多命名查询,当我在其中一个查询中输入错误时,看到我的应用程序启动日志文件中的错误类似于下面的一个.
Caused by: org.hibernate.HibernateException: Errors in named queries: FindAllCompanyFileTypes
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:426)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)
    ... 70 more

如何配置hibernate打印出命名查询有什么问题,而不只是命名查询错误

UPDATE例如JPA查询SELECT f FROM Foo WHERE f.v:= true将失败,而hibernate抱怨查询无效. Hibernate甚至没有尝试从它生成sql,查询是不正确的JPQL.我想知道的是如何让hibernate说查询错误的,因为:=被使用而不是=?不知道这是否是可以在休眠或未启动的设置.

解决方法

Hibernate自定义查询加载器的东西位于org.hibernate.loader.custom.sql(对于Hibernate 3,似乎是 Hibernate 4 too).如果使用 log4j,只需要设置此软件包 its own category获取日志打印(我建议您使用文件追加程序,因为如果使用控制台追加程序,以后的错误日志可能与您感兴趣的内容重叠).
<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
原文链接:https://www.f2er.com/java/122734.html

猜你在找的Java相关文章