spring data jpa中实现JpaRepository接口的repository可以返回sortedset吗?
Posted
技术标签:
【中文标题】spring data jpa中实现JpaRepository接口的repository可以返回sortedset吗?【英文标题】:Can a repository that implements the JpaRepository interface in spring data jpa return a sortedset? 【发布时间】:2012-02-09 00:38:33 【问题描述】:我正在使用 Spring Data JPA 1.0.1。我定义了一个 JPA 存储库。我正在使用嵌入式数据库实现编写集成测试。当我的服务类调用我的存储库时 我得到以下异常
result returns more than one elements
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:298)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:102)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:368)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy48.findMyEntities(Unknown Source)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
这是我的存储库实现
public interface MyRepository extends JpaRepository<MyEntity, Long>
@Transactional(readOnly = true)
@Query("select e from MyEntity e where e.state= :state")
public SortedSet<MyEntity> findMyEntities(@Param("state") EntityState state);
【问题讨论】:
你的 sql 是否返回多个元素? 是的。我更改了接口方法 findMyEntities 以返回 List我改了
@Transactional(readOnly = true)
@Query("select e from MyEntity e where e.state= :state")
public SortedSet<MyEntity> findMyEntities(@Param("state") EntityState state);
到
@Transactional(readOnly = true)
@Query("select e from MyEntity e where e.state= :state")
public List<MyEntity> findMyEntities(@Param("state") EntityState state);
一切正常。然后我添加了一个排序参数以确保正确排序。
【讨论】:
以上是关于spring data jpa中实现JpaRepository接口的repository可以返回sortedset吗?的主要内容,如果未能解决你的问题,请参考以下文章
是否可以使用 JpaRepository 在 Spring Data JPA 中使用“TRUNCATE”?还是比标准 deleteAll() 更有效的方法?