Querying in more than 1 field with FindBy in spring mvc

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 call Query 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;
    }
}

It is giving error when calling the function:

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

This part is red: findById_seletivoAndId_usuario

    
asked by anonymous 28.11.2018 / 14:01

1 answer

0

The two parameters of the findById_seletivoAndId_usuario method are of 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 type NivelUsuario and not Long .

So the problem.

Pass a Long instead of NivelUsuario and the error will no longer exist.

    
28.11.2018 / 18:47