使用ElementCollection排序的JPA查询给出UncertedOperationException

d6kp6zgx  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(153)

当有一个实体有一个简单类型的ElementCollection字段,并且试图使用该字段的排序进行查询时,会有一个UnsupportedOperationException

public class SomeParent {
  @ElementCollection
  private List<String> keywords;
}

public interface SomeParentRepository extends JpaRepository<SomeParent, Integer>, JpaSpecificationExecutor<SomeParent> {

  @Query("select sp from SomeParent sp")
  Page<SomeParent> findBySomething(Pageable pageable);
}

字符串
调用带有字段排序的自定义查询时,会出现错误:

var pageable = PageRequest.of(0, 20, Sort.by("keywords"));
someParentRepository.findBySomething(pageable);


但是,调用findAll()(按字段排序)工作正常:

var pageable = PageRequest.of(0, 20, Sort.by("keywords"));
someParentRepository.findAll(pageable);


并且调用自定义查询而不进行排序也可以正常工作。

var pageable = PageRequest.of(0, 20);
someParentRepository.findBySomething(pageable);


我尝试将@OrderBy添加到字段,但没有帮助。
我希望有一种不同于使用Specification的方法,因为实际查询有更多的参数。

java.lang.UnsupportedOperationException: null
    at org.hibernate.query.sqm.sql.internal.PluralValuedSimplePathInterpretation.accept(PluralValuedSimplePathInterpretation.java:72) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.visitSortSpecification(AbstractSqlAstTranslator.java:3764) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.visitSortSpecification(AbstractSqlAstTranslator.java:3745) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.renderOrderBy(AbstractSqlAstTranslator.java:3351) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.visitOrderBy(AbstractSqlAstTranslator.java:3335) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.visitQuerySpec(AbstractSqlAstTranslator.java:3086) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.dialect.PostgreSQLSqlAstTranslator.visitQuerySpec(PostgreSQLSqlAstTranslator.java:186) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.sql.ast.tree.select.QuerySpec.accept(QuerySpec.java:119) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.visitSelectStatement(AbstractSqlAstTranslator.java:988) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.translateSelect(AbstractSqlAstTranslator.java:851) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.sql.ast.spi.AbstractSqlAstTranslator.translate(AbstractSqlAstTranslator.java:801) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.buildCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:372) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.withCacheableSqmInterpretation(ConcreteSqmSelectQueryPlan.java:268) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.query.sqm.internal.ConcreteSqmSelectQueryPlan.performList(ConcreteSqmSelectQueryPlan.java:244) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.query.sqm.internal.QuerySqmImpl.doList(QuerySqmImpl.java:518) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.query.spi.AbstractSelectionQuery.list(AbstractSelectionQuery.java:367) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.hibernate.query.Query.getResultList(Query.java:119) ~[hibernate-core-6.2.13.Final.jar:6.2.13.Final]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution$PagedExecution.doExecute(JpaQueryExecution.java:204) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:92) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:149) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:137) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.doInvoke(RepositoryMethodInvoker.java:136) ~[spring-data-commons-3.1.5.jar:3.1.5]
    at org.springframework.data.repository.core.support.RepositoryMethodInvoker.invoke(RepositoryMethodInvoker.java:120) ~[spring-data-commons-3.1.5.jar:3.1.5]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.doInvoke(QueryExecutorMethodInterceptor.java:164) ~[spring-data-commons-3.1.5.jar:3.1.5]
    at org.springframework.data.repository.core.support.QueryExecutorMethodInterceptor.invoke(QueryExecutorMethodInterceptor.java:143) ~[spring-data-commons-3.1.5.jar:3.1.5]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.13.jar:6.0.13]
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:72) ~[spring-data-commons-3.1.5.jar:3.1.5]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.13.jar:6.0.13]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:123) ~[spring-tx-6.0.13.jar:6.0.13]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:391) ~[spring-tx-6.0.13.jar:6.0.13]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:119) ~[spring-tx-6.0.13.jar:6.0.13]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.13.jar:6.0.13]
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:137) ~[spring-tx-6.0.13.jar:6.0.13]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.13.jar:6.0.13]
    at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:135) ~[spring-data-jpa-3.1.5.jar:3.1.5]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.13.jar:6.0.13]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-6.0.13.jar:6.0.13]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.13.jar:6.0.13]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:244) ~[spring-aop-6.0.13.jar:6.0.13]
    at jdk.proxy2/jdk.proxy2.$Proxy248.findBySomething(Unknown Source) ~[na:na]

sczxawaw

sczxawaw1#

第一个月

Page<SomeParent> findBySomething(Pageable pageable);

eit6fx6z

eit6fx6z2#

我想你可以尝试使用“nativeQuery”进行排序。所以我给予给你一个例子:

@Query(value = "SELECT * FROM some_parent ORDER BY keywords ASC", nativeQuery = true)
Page<SomeParent> findBySomething(Pageable pageable);

字符串

相关问题