Un repository che implementa l'interfaccia JParePository in Spring Data JPA restituire un SordSet?

StackOverflow https://stackoverflow.com/questions/8839903

  •  27-10-2019
  •  | 
  •  

Domanda

Sto usando Spring Data JPA 1.0.1. Ho un repository JPA definito. Sto scrivendo un test di integrazione utilizzando un'implementazione di database incorporata. Quando la mia classe di servizio chiama il mio repository ricevo la seguente eccezione

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)

Ecco l'implementazione del mio repository

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);
}
È stato utile?

Soluzione

Ho cambiato il

@Transactional(readOnly = true)
@Query("select e from MyEntity e where e.state= :state")
public SortedSet<MyEntity> findMyEntities(@Param("state") EntityState state);

a

@Transactional(readOnly = true)
@Query("select e from MyEntity e where e.state= :state")
public List<MyEntity> findMyEntities(@Param("state") EntityState state);

E tutto ha funzionato. Ho quindi aggiunto un parametro di ordinamento per garantire l'ordinamento corretto.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top