How to create a custom findBy method for spring?

Asked

Viewed 394 times

0

I can use findById in the controller but wanted to know how to create a findByQualquer to another class property.

follows the code:

Model:

package com.leonardo.pokedex.model;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Objects;

@Document
public class Pokemon{

    @Id
    private String id;

    private String nome;
    private String categoria;
    private String habilidade;
    private Double peso;

    public Pokemon() {
        super();
    }

    public Pokemon(String id, String nome, String categoria, String habilidade, Double peso) {
        this.id = id;
        this.nome = nome;
        this.categoria = categoria;
        this.habilidade = habilidade;
        this.peso = peso;
    }

    public String getId() {
        return id;
    }

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

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getCategoria() {
        return categoria;
    }

    public void setCategoria(String categoria) {
        this.categoria = categoria;
    }

    public String getHabilidade() {
        return habilidade;
    }

    public void setHabilidade(String habilidade) {
        this.habilidade = habilidade;
    }

    public Double getPeso() {
        return peso;
    }

    public void setPeso(Double peso) {
        this.peso = peso;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Pokemon pokemon = (Pokemon) o;
        return Objects.equals(id, pokemon.id) &&
                Objects.equals(nome, pokemon.nome) &&
                Objects.equals(categoria, pokemon.categoria) &&
                Objects.equals(habilidade, pokemon.habilidade) &&
                Objects.equals(peso, pokemon.peso);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, nome, categoria, habilidade, peso);
    }

    @Override
    public String toString() {
        return "Pokemon{" +
                "id='" + id + '\'' +
                ", nome='" + nome + '\'' +
                ", categoria='" + categoria + '\'' +
                ", habilidade='" + habilidade + '\'' +
                ", peso=" + peso +
                '}';
    }
}

Repository:

package com.leonardo.pokedex.repository;

import com.leonardo.pokedex.model.Pokemon;
import org.springframework.data.mongodb.repository.ReactiveMongoRepository;

public interface PokemonRepository extends ReactiveMongoRepository <Pokemon, String>{

}
  • Dear Leonardo I recommend that in the future simplify the question to the problem in focus, it is not necessary to add everything, the ideal is to create a minimal functional example or then explain exactly where you need something to focus on this part, that in your case it is Repository that you needed a customized method. I edited the question this time to see if it helps you understand how to ask future questions, I hope the answer will help you, and the question will be useful to you.

1 answer

1


I recommend you read the documentation, there are several ways to create/adjust queries, see https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.query-Creation

In your interface you can create a query method:

public interface PokemonRepository extends ReactiveMongoRepository <Pokemon, String>{
     List<Pokemon> findByTypeAndRegion(String type, String region);
}

Or use @Query to customize the query as you see fit:

public interface PokemonRepository extends ReactiveMongoRepository <Pokemon, String>{
    @Query("select ... query complexa ORDER by ...")
    List<Pokemon> findByBestIV();
}

Or if you return only the best of all:

public interface PokemonRepository extends ReactiveMongoRepository <Pokemon, String>{
    @Query("select ... query complexa ORDER by ...")
    Pokemon findByBestIV();
}

Suppose you want a search by type of Pokémon and region to which it originates (ps: I don’t understand JPA, if something is incorrect you can comment)

  • had already read this documentation, but it didn’t work, I added the following code to Repository: List<Pokemon> findByCategory(String category); , and added this to Controller: @Getmapping("/category={category}") public List<Pokemon> getPokemonByCatergoria(@Pathvariable String category) { Return Repository.findByCategory(category); }

  • Dear @Leonardojuniorcavazzola "did not work" is ambiguous. Many things may not work for many reasons, you have to inform which error message has occurred.

  • I sent a reply, help me please.

  • It did not need to formulate an answer, the answer field is only for problem solving. Ok, try using @Query to customize how you want to filter your query.

Browser other questions tagged

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