Spring Data 2.1.1 org.hibernate.HibernateException:元组和别名的大小不同吗?

时间:2018-11-06 09:48:54

标签: java spring-boot spring-data-jpa

从Spring Boot的1.5.4升级到2.1.0 LTS

我在使用特定存储库方法时遇到了一些问题。

我检查了SO和JIRA上提到@Query@ResultsSetMapping的各种问题,并相应地应用了这些更改,但是我仍然面临着同样的问题。

例如此处:

存储库

@CrossOrigin
@RepositoryRestResource(path = "/iCreateChange")
public interface ICreateChangeRepository extends JpaRepository<ICreateChange, Long> {

  List<ChangeTask> listStartedChangesByInitiator(
        @Param("initiator")
        @RequestParam
        @ApiParam(name = "initiator", value = "initiator")
        String initiator);

  List<ChangeTask> listDataValuesAndStatusByChangeId(
        @Param("changeId")
        @RequestParam
        @ApiParam(name = "changeId", value = "changeId")
        Long changeId);
}

实体

@SqlResultSetMappings({
    @SqlResultSetMapping(
      name = "StartedChangeMapping",
      classes = @ConstructorResult(
        targetClass = ChangeTask.class,
        columns = {
          @ColumnResult(name = "id", type = Long.class),
          @ColumnResult(name = "status", type = String.class),
          @ColumnResult(name = "data_values", type = String.class)
        }
      )
    ),
    @SqlResultSetMapping(
      name = "ActiveTaskChangeMapping",
      classes = @ConstructorResult(
        targetClass = ChangeTask.class,
        columns = {
          @ColumnResult(name = "id", type = Long.class),
          @ColumnResult(name = "status", type = String.class),
          @ColumnResult(name = "data_values", type = String.class)
        }
      )
    )
})
@NamedNativeQueries({
    @NamedNativeQuery(
      name = "ICreateChange.listStartedChangesByInitiator",
      query =
      "SELECT id as id, status as status, data_values as data_values "
      + "FROM icreate_change "
      + "WHERE initiator = :initiator "
      + "AND workflow_process_id = 0",
      resultSetMapping = "StartedChangeMapping"
    ),
    @NamedNativeQuery(
      name = "ICreateChange.listDataValuesAndStatusByChangeId",
      query =
      "SELECT id as id, status as status, data_values as data_values "
      + "FROM icreate_change "
      + "WHERE id = :changeId",
      resultSetMapping = "ActiveTaskChangeMapping"
    )

})

@Data
@Entity
@Table(name = "icreate_change")
public class ICreateChange {

  public ICreateChange() {}
....

我错过了什么吗?

堆栈跟踪

org.hibernate.HibernateException: Got different size of tuples and aliases
    at org.hibernate.jpa.spi.NativeQueryTupleTransformer$NativeTupleImpl.<init>(NativeQueryTupleTransformer.java:68) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.jpa.spi.NativeQueryTupleTransformer.transformTuple(NativeQueryTupleTransformer.java:28) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.hql.internal.HolderInstantiator.instantiate(HolderInstantiator.java:85) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:433) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2506) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.loader.Loader.list(Loader.java:2501) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:338) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2223) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1053) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:170) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1505) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]
    at org.hibernate.query.Query.getResultList(Query.java:135) ~[hibernate-core-5.3.7.Final.jar:5.3.7.Final]

调试

调试NativeTupleImpl的实现表明,别名和元组在那里,但是存在嵌套问题。有一个具有所有值的元组对象,但是有3个不嵌套且作为三个实体的别名。造成不匹配的原因

enter image description here

参考:

https://jira.spring.io/si/jira.issueviews:issue-html/DATAJPA-1280/DATAJPA-1280.html

"Got different size of tuples and aliases" exception after Spring Boot 2.0.0.RELEASE migration

1 个答案:

答案 0 :(得分:0)

问题在于Hibernate默认情况下如何将其Tuple实体映射到一个Object或Objects列表中。

在这种情况下,期望三个元组映射到POJO中的各个列。

这是使其正常工作的代码(请注意,省略了@ConstructorResult

@SqlResultSetMapping(
        name = "TaskChangeMapping",
        columns = {
                @ColumnResult(name = "id", type = Long.class),
                @ColumnResult(name = "status", type = String.class),
                @ColumnResult(name = "data_values", type = String.class)

})