Kotlin JPA-使用参数值作为select的返回

Kotlin JPA-使用参数值作为select的返回,kotlin,spring-data-jpa,spring-data,jpql,Kotlin,Spring Data Jpa,Spring Data,Jpql,下面的示例代码更好地说明了我想要实现的目标。我希望能够将值注入到查询中,以便这些值已经存在于resultset中,而不必在resultset上循环以添加额外的数据 示例模型 data class ExampleModel( var transactionDate: Date? = null, var totalTransactionAmount: Long? = null, var totalTransactions: Long? = null, var paye

下面的示例代码更好地说明了我想要实现的目标。我希望能够将值注入到查询中,以便这些值已经存在于resultset中,而不必在resultset上循环以添加额外的数据

示例模型

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>

将值转换为字符串完成了此操作。类似于我在同一个查询中已经拥有的到目前为止的演员阵容

@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>
@Query(“从ExampleEntity te group by cast(te.dateCreated as date)、sum(te.amount)、count(te)、cast(:paramValue as string))中逐个cast(te.dateCreated as date)、te.transactionStatus选择新的example.model
趣味FindAggregateExamples(@Param(“paramValue”)paramValue:String):列表

为什么返回一个字符串文字,上面写着“:paramValue”?如果要返回参数(无论出于何种原因),请删除引号。为什么要使用附加的静态值调用select,以便在结果集中再次返回它。。。不知道这样的往返应该有什么好处…@BillyFrost-如果我删除引号,jpql会认为这是一个很长的值@罗兰如果我不这样做,我还有两个选择:1。使用一些if-else进行非常复杂的连接,并连接到多个表以检索paramValue 2。在resultset上循环并将paramValue添加到我的模型中。上面只是我举的一个例子,但实际上,这将用于按需提取报告,从长远来看,数据量可能是巨大的,这归结于“Spring data JPA”如何将内容传递给JPA API(在其他API之上使用API抽象的问题)。如果它认为这是一个“长”,那么在这种情况下,发布完整的例外,人们可以看到这是从哪里来的。
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
@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>