Ben
Ben

Reputation: 62424

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

}

Upvotes: 14

Views: 42348

Answers (2)

Mohammad Adil
Mohammad Adil

Reputation: 533

Try this it works

keep your model in list

@RequestMapping(value="/deleteDriver/{id}" , method=RequestMethod.POST)
public ResponseEntity<Object> deleteDriver(@PathVariable("id") Integer id)
{
    List<Driver> delete_driver=adminService.getDriverById(id);
    Map<String,Object> response=new HashMap<>();


    if(delete_driver==null)
    {
        response.put("status", "Failure");
        return new ResponseEntity<Object>(response,HttpStatus.NO_CONTENT);
    }
    else
    {
        response.put("status", "Success");
        adminService.delete(delete_driver);
        return new ResponseEntity<Object>(response,HttpStatus.OK);
    }

}

then in your repository

@Override
public void delete(List<Driver> delete_driver) {

    driverRepository.delete(delete_driver);
}

Upvotes: 0

Ben
Ben

Reputation: 62424

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!

Upvotes: 9

Related Questions