Un référentiel qui implémente l'interface JParePository dans les données de printemps JPA renvoie un sortset?
-
27-10-2019 - |
Question
J'utilise Spring Data JPA 1.0.1. J'ai un référentiel JPA défini. J'écris un test d'intégration à l'aide d'une implémentation de base de données intégrée. Lorsque ma classe de service appelle mon référentiel, j'obtiens l'exception suivante
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)
Voici mon implémentation de référentiel
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);
}
La solution
J'ai changé le
@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);
Et tout a fonctionné. J'ai ensuite ajouté un paramètre de tri pour assurer un tri approprié.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow