NoSuchMethodException with" select new"升级到Spring Data JPA 1.10.3之后

时间:2016-09-27 16:10:27

标签: spring-data-jpa openjpa

我将Spring Data JPA从1.9.2升级到1.10.3后遇到了问题。

我有以下存储库:

public interface ShipmentStatisticRepository extends JpaRepository<Shipment, Long> {

    ...

    @Query("select new de.l.b.model.dto.shipment.ShipmentStatistic(s.createdYmd, count(distinct s.oid), count(op), sum(op.price)) " +
            "from Shipment s " +
            "join s.orderPosition op " +
            "where s.created between ?1 and ?2 " +
            "group by s.createdYmd")
    List<ShipmentStatistic> findPerDayBetweenCreated(Date from, Date to);

    ...
}

当我调用此方法时,我得到以下异常:

 org.springframework.dao.InvalidDataAccessApiUsageException: Could not instantiate Collection type: org.apache.openjpa.kernel.DelegatingResultList; nested exception is java.lang.IllegalArgumentException: Could not instantiate Collection type: org.apache.openjpa.kernel.DelegatingResultList
    at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:384)
    at org.springframework.orm.jpa.DefaultJpaDialect.translateExceptionIfPossible(DefaultJpaDialect.java:122)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:436)
    at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
    at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:133)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy154.findPerDayBetweenCreated(Unknown Source)
    at de.l.b.hazelcast.map.ShipmentStatisticPerDayMapLoader.getShipmentStatistic(ShipmentStatisticPerDayMapLoader.java:40)
    at de.l.b.hazelcast.map.AbstractShipmentStatisticMapLoader.load(AbstractShipmentStatisticMapLoader.java:38)
    at de.l.b.hazelcast.map.AbstractShipmentStatisticMapLoader.loadAll(AbstractShipmentStatisticMapLoader.java:48)
    at com.hazelcast.map.impl.MapStoreWrapper.loadAll(MapStoreWrapper.java:143)
    at com.hazelcast.map.impl.mapstore.AbstractMapDataStore.loadAll(AbstractMapDataStore.java:56)
    at com.hazelcast.map.impl.mapstore.writebehind.WriteBehindStore.loadAll(WriteBehindStore.java:205)
    at com.hazelcast.map.impl.recordstore.BasicRecordStoreLoader.loadAndGet(BasicRecordStoreLoader.java:170)
    at com.hazelcast.map.impl.recordstore.BasicRecordStoreLoader.doBatchLoad(BasicRecordStoreLoader.java:143)
    at com.hazelcast.map.impl.recordstore.BasicRecordStoreLoader.loadValuesInternal(BasicRecordStoreLoader.java:129)
    at com.hazelcast.map.impl.recordstore.BasicRecordStoreLoader.access$100(BasicRecordStoreLoader.java:54)
    at com.hazelcast.map.impl.recordstore.BasicRecordStoreLoader$GivenKeysLoaderTask.call(BasicRecordStoreLoader.java:112)
    at com.hazelcast.util.executor.CompletableFutureTask.run(CompletableFutureTask.java:67)
    at com.hazelcast.util.executor.CachedExecutorServiceDelegate$Worker.run(CachedExecutorServiceDelegate.java:212)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    at com.hazelcast.util.executor.HazelcastManagedThread.executeRun(HazelcastManagedThread.java:76)
    at com.hazelcast.util.executor.HazelcastManagedThread.run(HazelcastManagedThread.java:92)
Caused by: java.lang.IllegalArgumentException: Could not instantiate Collection type: org.apache.openjpa.kernel.DelegatingResultList
    at org.springframework.core.CollectionFactory.createCollection(CollectionFactory.java:208)
    at org.springframework.core.CollectionFactory.createCollection(CollectionFactory.java:151)
    at org.springframework.data.repository.query.ResultProcessor.processResult(ResultProcessor.java:146)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:119)
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:104)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:482)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:460)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    ... 25 more
Caused by: java.lang.InstantiationException: org.apache.openjpa.kernel.DelegatingResultList
    at java.lang.Class.newInstance(Class.java:427)
    at org.springframework.core.CollectionFactory.createCollection(CollectionFactory.java:204)
    ... 39 more
Caused by: java.lang.NoSuchMethodException: org.apache.openjpa.kernel.DelegatingResultList.<init>()
    at java.lang.Class.getConstructor0(Class.java:3082)
    at java.lang.Class.newInstance(Class.java:412)
    ... 40 more

看起来存储库无法解析正确的构造函数来填充我的投影。使用1.9.2版本,一切正常。

这是我的投影课:

public class ShipmentStatistic implements Serializable {

    private Date date;
    private long totalParcelsSent;
    private long totalPositionsSent;
    private long totalValue;

    public ShipmentStatistic(Date date) {
        this.date = date;
        this.totalPositionsSent = 0;
        this.totalParcelsSent = 0;
        this.totalValue = 0;
    }

    public ShipmentStatistic(Date date, long totalParcelsSent, long totalPositionsSent, long totalValue) {
        this.date = date;
        this.totalPositionsSent = totalPositionsSent;
        this.totalParcelsSent = totalParcelsSent;
        this.totalValue = totalValue;
    }

    public ShipmentStatistic add(ShipmentStatistic other) {
        this.totalPositionsSent += other.totalPositionsSent;
        this.totalParcelsSent += other.totalParcelsSent;
        this.totalValue += other.totalValue;

        return this;
    }

    // getter and setter
}  

我随机拍摄并尝试添加默认构造函数,但没有成功。

我无法看到我的错误,尤其是所做的唯一更改是框架更新。顺便说一下,我们使用OpenJPA 2.2.2作为Persistence API。我升级到当前版本2.4.1,但这也没有效果。

有什么建议吗?这可能是Spring Data JPA与OpenJPA结合的错误吗?

1 个答案:

答案 0 :(得分:0)

从stacktrace开始,看起来openjpa正在搜索默认构造函数。您可以尝试在 ShipmentStatistic.java

中明确添加默认构造函数

JPA规范要求使用默认的无参数构造函数。 http://openjpa.apache.org/builds/1.2.3/apache-openjpa/docs/jpa_overview_pc.html#jpa_overview_pc_no_arg