Searching more than 1 field with Findby in spring mvc

Asked

Viewed 202 times

0

I have this repository:

public interface RepositorioUsuarioPermissao extends JpaRepository<UsuarioPermissao, Long> {

    @Query("SELECT u FROM UsuarioPermissao u WHERE u.id_seletivo = ?1 and u.id_usuario = ?2")
    List<UsuarioPermissao> findById_seletivoAndId_usuario(Long id_seletivo, Long id_usuario);    
}

I make the Query call above like this:

@RequestMapping(value = "/adicionar", method = RequestMethod.POST)
public String adicionar(@ModelAttribute("usuariopermissao") UsuarioPermissao usuariopermissaoNovo, Model model) {
      List<UsuarioPermissao> usuariopermissaolista = repositorioUsuarioPermissao.
      findById_seletivoAndId_usuario(usuariopermissaoNovo.getSeletivo(),
      usuariopermissaoNovo.getNivelusuario()); // para buscar só o seletivo e usuario selecionado
}

I have the Entity:

public class UsuarioPermissao {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "upm_id")
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER )
    @JoinColumn(name = "upm_id_usuario")
    private Pessoa pessoa;

    @ManyToOne(fetch = FetchType.EAGER )
    @JoinColumn(name = "upm_id_seletivo")
    private Seletivo seletivo;    

    @ManyToOne(fetch = FetchType.EAGER )
    @JoinColumn(name = "upm_id_permissao")
    private NivelUsuario nivelusuario;    


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }



    public NivelUsuario getNivelusuario() {
        return nivelusuario;
    }

    public void setNivelusuario(NivelUsuario nivelusuario) {
        this.nivelusuario = nivelusuario;
    }

    public Pessoa getPessoa() {
        return pessoa;
    }

    public void setPessoa(Pessoa pessoa) {
        this.pessoa = pessoa;
    }


    public Seletivo getSeletivo() {
        return seletivo;
    }

    public void setSeletivo(Seletivo seletivo) {
        this.seletivo = seletivo;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((id == null) ? 0 : id.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        UsuarioPermissao other = (UsuarioPermissao) obj;
        if (id == null) {
            if (other.id != null)
                return false;
        } else if (!id.equals(other.id))
            return false;
        return true;
    }
}

Is giving error when calling the function:

List<UsuarioPermissao> usuariopermissaolista = repositorioUsuarioPermissao.
      findById_seletivoAndId_usuario(usuariopermissaoNovo.getSeletivo(),
      usuariopermissaoNovo.getNivelusuario());

Turn this part red: findById_seletivoAndId_usuario

  • The method findById_seletivoAndId_usuario receives two parameters of the type Long. But getSeletivo() and getNivelusuario() do not return a Long, and yes instances of Seletivo and NivelUsuario (who are not Long), hence the error

  • What error is occurring?

1 answer

0

The two parameters of the method findById_seletivoAndId_usuario are of the type Long.

However, in the call you are passing to the second parameter the result of usuariopermissaoNovo.getNivelusuario(), but the return of this method is of the type NivelUsuario and not Long.

That’s why the problem.

Pass a Long instead of NivelUsuario and the error will cease to exist.

Browser other questions tagged

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