Java – how to make hibernate print out named queries? What’s the problem?

In my spring / Hibernate / JPA application, I use many named queries. When I enter an error in one of the queries, I see the error in my application startup log file, which is similar to the following one

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

How to configure hibernate to print out the problems with named queries, not just errors in named queries?

For example, JPA query select f from foo where F.V: = true will fail, while hibernate complains that the query is invalid Hibernate didn't even try to generate SQL from it, and the query is incorrect jpql What I want to know is how to make hibernate say that the query is wrong because: = is used instead of =? I don't know whether this can be set in hibernation or not

Solution

The hibernate custom query loader is located at org hibernate. loader. custom. SQL (for hibernate 3, it seems to be hibernate 4 too) If log4j is used, you only need to set this package its own category to obtain log printing (I suggest you use the file append program, because if you use the console append program, the future error log may overlap with the content you are interested in)

<category name="org.hibernate.loader.custom.sql" additivity="false">
    <priority value="trace" />
    <appender-ref ref="fileAppender" />
</category>

Suppose your root logger displays every error in the file, which is the output of an error when the query is loaded:

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)

Exceptions are being thrown while loading, but query errors may also occur when executing code In this case, a HibernateException or similar is thrown, which you can check later The missing colon is actually the illegalargumentexception class thrown by abstractqueryimpl:

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
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>