当我在我的存储库方法中使用List类型的参数时,我遇到了不稳定的错误。这个问题似乎与Kotlin列表和Java之间的互操作性有关。当我运行单元测试时,它会随机失败,我怀疑这与Kotlin中由listOf()返回的MutableList有关。更多信息可以在这个StackOverflow帖子中找到:listOf() returns MutableList。
这是我的单元测试和存储库方法的示例:
@Test
fun `should return recipes with the specified author, locale and categories`() {
println("AuthorID: " + author.id)
recipeRepo.findRecipesBy(
locale = LanguageSelection.ENGLISH,
authorIds = arrayListOf(44),
)
// it throws before reaching asserts
}
代码库:
interface RecipeRepository : JpaRepository<Recipe, Long> {
@Query("SELECT r FROM Recipe r WHERE "
+ "(:authorIds is null or r.author.id in (:authorIds)) "
+ "and (:recipeIds is null or r.id in (:recipeIds)) "
+ "and (:minPrice is null or r.estimatedPrice >= :minPrice) "
+ "and (:maxPrice is null or r.estimatedPrice <= :maxPrice) "
+ "and ( cast(:beforeDate as timestamp) is null or r.createdAt < cast(:beforeDate as timestamp)) "
+ "and ( cast(:afterDate as timestamp) is null or r.createdAt > cast(:afterDate as timestamp)) "
+ "and (:minLikeCount is null or r.likeCount >= :minLikeCount) "
+ "and (:categoryIds is null or exists(select rc from r.categories rc where rc.id in (:categoryIds))) "
)
fun findRecipesBy(
@Param("authorIds") authorIds: ArrayList<Long>? = null,
@Param("recipeIds") recipeIds: ArrayList<Long>? = null,
@Param("minPrice") minPrice: Double? = null,
@Param("maxPrice") maxPrice: Double? = null,
@Param("beforeDate") beforeDate: Date? = null,
@Param("afterDate") afterDate: Date? = null,
@Param("minLikeCount") minLikeCount: Int? = null,
@Param("categoryIds") categoryIds: ArrayList<Long>? = null,
sort: Sort = Sort.by(Sort.Direction.ASC, "createdAt"),
): List<Recipe>
}
错误:
Parameter value [[44]] did not match expected type [BasicSqmPathSource(id : Long) ]
org.springframework.dao.InvalidDataAccessApiUsageException: Parameter value [[44]] did not match expected type [BasicSqmPathSource(id : Long) ]
at app//org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:371)
at app//org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:235)
at app//org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:550)
at app//org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at app//org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at app//org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152)
at app//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at app//org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:134)
at app//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at app//org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at app//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at app//org.springframework.data.repository.core.support.MethodInvocationValidator.invoke(MethodInvocationValidator.java:94)
at app//org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184)
at app//org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:218)
at app/jdk.proxy3/jdk.proxy3.$Proxy189.findRecipesBy(Unknown Source)
at app//com.fittastetic.fittastetic_backend.shared.recipe.repository.RecipeRepository$DefaultImpls.findRecipesBy$default(RecipeRepository.kt:38)
...
Caused by: org.hibernate.type.descriptor.java.CoercionException: Cannot coerce value `[44]` [java.util.ArrayList] as Long
at app//org.hibernate.type.descriptor.java.LongJavaType.coerce(LongJavaType.java:155)
at app//org.hibernate.type.descriptor.java.LongJavaType.coerce(LongJavaType.java:24)
at app//org.hibernate.query.internal.QueryParameterBindingImpl.coerce(QueryParameterBindingImpl.java:144)
at app//org.hibernate.query.internal.QueryParameterBindingImpl.setBindValue(QueryParameterBindingImpl.java:111)
... 141 more