Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Repository: javax.persistence.NonUniqueResultException: result returns more than one elements

Using the below code I am unable to get the results of my query. Whether I use Map<ContentType... or Map<String... I get the same error: javax.persistence.NonUniqueResultException: result returns more than one elements

It seems like JPA should be able to handle multiple rows in Repositories. I've looked around for other annotations that I might just be missing and am having a hard time coming up with results.

Any suggestions on what I should do to resolve this?

@Transactional
public interface ContentRepository extends JpaRepository<Content,Integer>{

    ....

    @Query(nativeQuery=true, value="SELECT content_type, COUNT(*) AS myColumn FROM dbo.content GROUP BY content_type")
    Map<ContentType, Integer> getContentCountByType();

}
like image 304
Webnet Avatar asked Sep 04 '12 20:09

Webnet


1 Answers

It appears that the problem was that Map<ContentType, Integer> does not have a promise of a unique index, so JPA doesn't like mapping to it. By using List<Map<ContentType, Integer>> instead, it works great!

like image 176
Webnet Avatar answered Sep 24 '22 13:09

Webnet