在Spring Boot 2.0.0.RELEASE迁移之后,“获得了不同大小的元组和别名”异常

时间:2018-03-01 18:09:35

标签: java spring hibernate jpa spring-boot

//imports, etc.

@Entity
@Table(name = "TSTRANS")
@SqlResultSetMappings(
        {                   
                @SqlResultSetMapping(name = TS_TRANS_EMP_STAT,
                        classes = {
                                @ConstructorResult(
                                        targetClass = EmpStat.class,
                                        columns = {
                                                @ColumnResult(name = "EMPID", type = Long.class),
                                                @ColumnResult(name = "CODE", type = String.class),
                                                @ColumnResult(name = "TOTALCOUNT", type = Integer.class)
                                        }
                                )
                        })
        }
)
@NamedNativeQueries({
        @NamedNativeQuery(name = "TsTrans.getStat", query = "select * from SP_TASK_STATS_EMP  (:in_empid, :in_gidstr, :in_onlytodo)", resultSetMapping = TS_TRANS_EMP_STAT)
})
public class TsTrans extends TsTransCommon {
    public static final String TSTRANS_BADGE = "TSTRANS_BADGE";

    private static final long serialVersionUID = -3391028108003625153L;
    public static final String TS_TRANS_EMP_STAT = "TsTrans.empStat";
    public static final String TS_TRANS_SCHEDULE_STAT = "TsTrans.getScheduleStat";
    public static final String TS_TRANS_FOLLOWUP = "TS_TRANS_FOLLOWUP";
}

这是实体。

// imports, etc.

public class EmpStat extends BaseStat {

    private static final long serialVersionUID = -4410895509438727581L;
    private Long mEmpid;

    public EmpStat(Long aEmpid, String aCode, Integer aTotalcount) {
        super(aCode, aTotalcount);
        mEmpid = aEmpid;
    }

    public Long getEmpid() {
        return mEmpid;
    }

    public void setEmpid(Long aEmpid) {
        mEmpid = aEmpid;
    }
}

这是非实体pojo,namedquery返回类型。

// imports, etc.

@Repository
public interface TsTransRepository extends TsTransCommonRepository<TsTrans> {
    List<EmpStat> getStat(@Param("in_empid") Long aEmpid, @Param("in_gidstr") String aGidstr, @Param("in_onlytodo") Boolean aOnlytodo);

}

这是存储库类。

我正在使用此 namednativequeries 从存储过程返回非实体pojos。自Spring Boot 1.5.9以来,它一直在运行。并在Spring Boot 2.0.0.M7上。迁移到2.0.0.RELEASE后,开始发生以下异常。

org.hibernate.HibernateException: Got different size of tuples and aliases
    at org.hibernate.jpa.spi.NativeQueryTupleTransformer$NativeTupleImpl.<init>(NativeQueryTupleTransformer.java:68) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.jpa.spi.NativeQueryTupleTransformer.transformTuple(NativeQueryTupleTransformer.java:28) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.hql.internal.HolderInstantiator.instantiate(HolderInstantiator.java:85) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:430) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2507) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.loader.Loader.list(Loader.java:2502) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:335) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2161) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1016) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:152) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1414) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.hibernate.query.Query.getResultList(Query.java:146) ~[hibernate-core-5.2.14.Final.jar:5.2.14.Final]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:129) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:91) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:136) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:125) ~[spring-data-jpa-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:590) ~[spring-data-commons-2.0.5.RELEASE.jar:2.0.5.RELEASE]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:578) ~[spring-data-commons-2.0.5.RELEASE.jar:2.0.5.RELEASE]

在迁移期间,我是否遗漏了什么?

7 个答案:

答案 0 :(得分:12)

在jpa 2.1中,将@Query(nativeQuery = true)批注放在存储库接口方法上,解决了该问题。 参考:

https://github.com/spring-projects/spring-data-examples/tree/master/jpa/jpa21#support-for-custom-sqlresultsetmapping-with-constructorresult

答案 1 :(得分:8)

SqlResultSetMappings更改为

@SqlResultSetMappings({
    @SqlResultSetMapping(name = TS_TRANS_EMP_STAT,
        columns = {
            @ColumnResult(name = "EMPID", type = Long.class),
            @ColumnResult(name = "CODE", type = String.class),
            @ColumnResult(name = "TOTALCOUNT", type = Integer.class)
        })
}

并将EmpStat从普通类更改为接口:

public interface EmpStat {
    Long getEMPID();
    String getCODE();
    Integer getTOTALCOUNT();
}

答案 2 :(得分:6)

我找到了另一个解决方案:你可以在getStat()中删除List中的泛型:

@Repository
public interface TsTransRepository extends TsTransCommonRepository<TsTrans> {
    List getStat(@Param("in_empid") Long aEmpid, @Param("in_gidstr") String aGidstr, @Param("in_onlytodo") Boolean aOnlytodo);
}

答案 3 :(得分:5)

有几种方法可以解决它,主要是它是不同的jpa和spring数据功能的组合。我做了一些调查,并将它们添加到问题https://jira.spring.io/browse/DATAJPA-1280。要查找您可以执行的操作,请查看此项目https://github.com/EugeneNik/spring-data-datajpa-1280-example并运行测试以查看现在哪些方法正常工作。注意,目前没有代码更改就无法进行迁移,但在我看来,最简单的方法是将类投影声明添加到存储库方法。定义它您不必更改所有映射,但也必须更改存储库调用。这只是解决问题的另一种方法:

@Repository
public interface TsTransRepository extends TsTransCommonRepository<TsTrans> 
{
    <T> List<T> getStat(@Param("in_empid") Long aEmpid, 
@Param("in_gidstr") String aGidstr, @Param("in_onlytodo") Boolean aOnlytodo, Class<T> beanProjection);

}

答案 4 :(得分:2)

这是一个错误,现已修复: jira.spring.io/browse/DATAJPA-1280

添加:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="tableData-performance-week1"></table>

位于存储库中新方法的顶部。

答案 5 :(得分:1)

我认为这是Spring Data JPA中的问题报告:https://jira.spring.io/browse/DATAJPA-1280

作为一种解决方法,您可以降级到Spring Data版本系列Kay-SR4(SR5是当前最新版本,是Spring Boot 2.0.0中使用的版本)。只需添加:

<spring-data-releasetrain.version>Kay-SR4</spring-data-releasetrain.version>

到你的pom.xml的<properties>部分。

答案 6 :(得分:0)

正如其他人指出的那样,这是Spring Boot 2.0.0中引入的错误,并在DATAJPA-1280中进行了报告。

它已在Spring Boot 2.0.3中修复并发布。