在spring数据jpa中实现JpaRepository接口的存储库是否可以返回一个sortedset?

时间:2012-01-12 17:59:18

标签: java spring spring-data-jpa

我正在使用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);
}

1 个答案:

答案 0 :(得分:3)

我改变了

@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);

一切顺利。然后我添加了一个sort参数以确保正确排序。