JPA - 使用param值作为select的回报

问题描述 投票:1回答:1

下面的示例代码更好地说明了我想要实现的目标。我希望能够为我的查询注入值,因此这些值已经在resultset中,而不是必须循环Resultset来添加额外的数据。

ExampleModel

data class ExampleModel(
    var transactionDate: Date? = null,
    var totalTransactionAmount: Long? = null,
    var totalTransactions: Long? = null,
    var payerName: String? = null
)

试试1:

@Query("select new example.model.ExampleModel(cast(te.dateCreated as date), sum(te.amount), count(te), :paramValue) from ExampleEntity te group by cast(te.dateCreated as date), te.transactionStatus")
fun findAggregatedExamples(@Param("paramValue") paramValue: String): List<ExampleModel>

服务器启动时的堆栈跟踪:

引起:java.lang.IllegalArgumentException:org.hibernate.hql.internal.ast.QuerySyntaxException:无法在类[example.model.ExampleModel]上找到适当的构造函数。预期的参数是:java.util.Date,long,long,long [select new example.model.ExampleModel(cast(tr.dateCompleted as date),sum(te.amount),count(te),:payerName)from example .entity.ExampleEntity te by cast(te.dateCreated as date),te.transactionStatus]

试试2:

@Query("select new example.model.ExampleModel(cast(te.dateCreated as date), sum(te.amount), count(te), ':paramValue') from ExampleEntity te group by cast(te.dateCreated as date), te.transactionStatus")
    fun findAggregatedExamples(@Param("paramValue") paramValue: String): List<ExampleModel>

运行时的Stacktrace:

Caused by: java.lang.IllegalArgumentException: Parameter with that name [payerName] did not exist
    at org.hibernate.jpa.spi.BaseQueryImpl.findParameterRegistration(BaseQueryImpl.java:487) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.spi.BaseQueryImpl.setParameter(BaseQueryImpl.java:638) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:163) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.hibernate.jpa.spi.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:32) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:139) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.StringQueryParameterBinder.bind(StringQueryParameterBinder.java:61) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.ParameterBinder.bind(ParameterBinder.java:101) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.SpelExpressionStringQueryParameterBinder.bind(SpelExpressionStringQueryParameterBinder.java:76) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:161) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.ParameterBinder.bindAndPrepare(ParameterBinder.java:152) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.AbstractStringBasedJpaQuery.doCreateQuery(AbstractStringBasedJpaQuery.java:81) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.createQuery(AbstractJpaQuery.java:202) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$CollectionExecution.doExecute(JpaQueryExecution.java:125) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:89) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:128) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:118) ~[spring-data-jpa-1.11.14.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:494) ~[spring-data-commons-1.13.14.RELEASE.jar:na]
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:477) ~[spring-data-commons-1.13.14.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:56) ~[spring-data-commons-1.13.14.RELEASE.jar:na]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136) ~[spring-tx-4.3.18.RELEASE.jar:4.3.18.RELEASE]
    ... 121 common frames omitted
kotlin spring-data-jpa spring-data jpql
1个回答
1
投票

将值转换为字符串就可以了。类似于我在同一个查询中已经拥有的演员!

@Query("select new example.model.ExampleModel(cast(te.dateCreated as date), sum(te.amount), count(te), cast(:paramValue as string)) from ExampleEntity te group by cast(te.dateCreated as date), te.transactionStatus")
    fun findAggregatedExamples(@Param("paramValue") paramValue: String): List<ExampleModel>
© www.soinside.com 2019 - 2024. All rights reserved.