How to get hibernate to print out whats wrong with a named query?
在我的 Spring / Hibernate / JPA 应用程序中,我使用了很多命名查询,当我在其中一个查询中出现拼写错误时,会在我的应用程序启动日志文件中看到类似于下面的错误。
1
2 3 4 5 |
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 查询
Hibernate 自定义查询加载器的内容位于
1
2 3 4 |
<category name="org.hibernate.loader.custom.sql" additivity="false">
<priority value="trace" /> </category> |
假设您的根记录器显示文件中的每个错误,这就是您在查询加载时得到的错误输出:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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:
1
2 3 4 5 6 7 |
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 |
我正在处理一个类似的问题,我发现,如果您使用 Spring Data JPA,您可以在 DAO 接口中使用 @Query 而不是在实体对象中使用 @NamedQuery,您会收到更详细的错误消息.所以,而不是:
1
2 3 4 5 6 7 |
@Entity
@Table @NamedQueries({@NamedQuery(name="MyEntity.myQuery" query="select blah blah blah")}) public class MyEntity { … } |
你会说
1
2 3 4 5 6 7 8 9 10 11 12 13 |
@Entity @Table public class MyEntity { … } public interface MyEntityDao |
这使用相同的查询语言和表达式,但是,无论实现的怪癖如何,都会提供更具表现力的错误消息。
如果我猜测你在问什么,我认为 hibernate 不能告诉你什么是错误的。我成功的唯一方法是相当痛苦,但至少它有效!
所以你需要数据库本身来告诉你查询出了什么问题。为此,您可以如上所述从hibernate中启用 sql 输出。然后你通过你最喜欢的工具连接到数据库:squirrelSQL、toad、eclipse-plugin 等。现在如果你的 sql 中没有参数,那么你只需剪切并粘贴到那里,执行 sql,它应该会帮助你调试。
如果你有参数,过程是一样的,只是更乏味!然而,一旦你的 SQL 没问题,数据库就会告诉你它不喜欢什么。在大多数情况下,一旦你知道了这一点,你就会清楚在hibernate端需要改变什么。虽然有时这可能是一个谜,但大多数情况都记录在这里!
您可以尝试通过设置
来打印HSQL
1
|
<property name="show_sql">true</property>
|
然后在 log4j 中添加适当的日志级别以显示 org.hibernate 的 DEBUG 或 TRACE 级别日志记录。*
更多信息在这里:http://docs.jboss.org/hibernate/core/3.5/reference/en/html/session-configuration.html#configuration-logging
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/java/267571.html