Hibernate多级JOIN FETCH导致在ResultSet异常中找不到列名

时间:2018-02-16 03:35:36

标签: spring hibernate spring-boot join fetch

我有一个查询试图完全加载实体:

SELECT DISTINCT i, ifi, iti FROM Item i 
LEFT JOIN FETCH i.inboundLinks iil 
LEFT JOIN FETCH iil.fromItem ifi 
LEFT JOIN FETCH i.outboundLinks iol 
LEFT JOIN FETCH iol.toItem iti 
LEFT JOIN FETCH i.itemValues 
LEFT JOIN FETCH ifi.inboundLinks ifiil 
LEFT JOIN FETCH ifiil.fromItem ififi 
LEFT JOIN FETCH ifi.outboundLinks ifiol 
LEFT JOIN FETCH ifiol.toItem ifiti 
LEFT JOIN FETCH ifi.itemValues 
LEFT JOIN FETCH iti.inboundLinks itiil 
LEFT JOIN FETCH itiil.fromItem itifi 
LEFT JOIN FETCH iti.outboundLinks itiol 
LEFT JOIN FETCH itiol.toItem ititi 
LEFT JOIN FETCH iti.itemValues 
WHERE i.itemId = :itemId

正如您所看到的那样,它遍历多个级别,通过不同路径中的相同实体。

执行此操作时,我收到以下错误:

Caused by: org.postgresql.util.PSQLException: The column name link_id1_11_1_ was not found in this ResultSet.
    at org.postgresql.jdbc.PgResultSet.findColumn(PgResultSet.java:2588) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.postgresql.jdbc.PgResultSet.getLong(PgResultSet.java:2486) ~[postgresql-9.4.1212.jre7.jar:9.4.1212.jre7]
    at org.hibernate.type.descriptor.sql.BigIntTypeDescriptor$2.doExtract(BigIntTypeDescriptor.java:63) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:238) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:234) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:224) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:300) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:789) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:714) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.processResultSet(Loader.java:972) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.doQuery(Loader.java:930) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.loader.Loader.doList(Loader.java:2617) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
    ... 92 common frames omitted

我可以在生成的sql中看到问题是正在查找的列的名称在末尾缺少下划线。这是生成的sql的相关片段:

SELECT DISTINCT
  item0_.item_id               AS item_id1_9_0_,
  item2_.item_id               AS item_id1_9_1_,
  item4_.item_id               AS item_id1_9_2_,
  ...
  inboundlin6_.to_item_id      AS to_item_9_11_1__,
  inboundlin6_.link_id         AS link_id1_11_1__,   ---- problem line
  item7_.transaction_id        AS transact2_9_6_,

我已经查看了与此错误消息相关的无数问题,并且几乎所有问题都建议使用非默认命名策略。我这样做是通过将这一行添加到application.properties(我使用带有hibernate的Spring-Boot):

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

这似乎没有任何改变。无论如何,它看起来像hibernate代码中只有一个错误。

是否有人知道这个问题的解决方案?

0 个答案:

没有答案