sql result in spring boot

Asked

Viewed 239 times

0

Good afternoon,

I need help to make an endpoint in Spring Boot, in the database (Mysql), I have the following query:

SELECT U.NOME, P.DESCRICAO FROM USUARIO_PERMISSAO UP 
    JOIN USUARIO U ON UP.ID_USUARIO = U.ID 
    JOIN PERMISSAO P ON UP.ID_PERMISSAO = P.ID

The table Usuario_permission, it is created by the bank, Manytomany of the user table and permission, I would like to know how I make an endpoint that I can bring the result.

As I don’t have this "model", I’m not managing to make the return of the Pository.

I’m trying to do it like this:

@Query(nativeQuery = true, value = "SELECT U.NOME, P.DESCRICAO FROM USUARIO_PERMISSAO UP JOIN USUARIO U ON UP.ID_USUARIO = U.ID JOIN PERMISSAO P ON UP.ID_PERMISSAO = P.ID") 
Object findByUsuarioPermissao();

@GetMapping
public Object findUsuarioPermissao() {
    return usuarioRepository.findByUsuarioPermissao();
}

But when testing is appearing this error:

"message": "query did not return a unique result: 20; nested exception is javax.persistence.NonUniqueResultException: query did not return a unique result: 20"
  • Hi, what have you tried to do in terms of code?

  • 1

    do us a favor? edit the question and enter these code from above. Thanks.

  • According to the error this query is returning a list, it has two ways to solve, making the return a List<Object> or put a LIMIT 1 in the sql query. PS recormendo do the Entity of this table, it is much easier to work, I do not know if the Object will work because n is serializable.

1 answer

4


The mistake is because the method findByUsuarioPermissao expects the return of an object only and its query is returning more than one object. The ideal would be to change to List<Object> or limit your query to bring only one result.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.