Search code examples
javamysqlspring-bootspring-data-jpafindby

Spring-Boot Data JPA findByField does not work on MySQL table REST


I want to query in Java Spring Boot JPA for risk parameter of type enum in my Virus table:

public interface VirusRepository extends JpaRepository<Virus, Long> {

    List<Virus> findByRisk(@RequestParam("risk") String risk);
}

this is my Virus class:

@Entity
@Table(name = "virus")
@Data
public class Virus {

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

    @Column(name = "name")
    private String name;

    @Column(name = "mortality_rate")
    private float mortality_rate;

    @Column(name = "risk")
    private String risk;

    @Column(name = "year")
    private int year;

    @ManyToOne
    @JoinColumn(name = "treatment_id", nullable = false)
    private Treatment treatment;
}

My treatment table: (virus has fk-key treatment id)

@Entity
@Table(name = "treatment")
@Data
public class Treatment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name = "name")
    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "treatment")
    private Set<Virus> viruses;
}

However, I do not get the desired data returned when I enter url: http://localhost:9999/api/viruses/?risk=high

why? is it maybe because I defined risk of type enum on my MySQL Table Virus entity?:

http://localhost:9999/api/viruses/?name=SARS

**EDIT: I just found it out myself, I forgot that the link is:

http://localhost:9999/api/viruses/search/findByRisk?risk=high

then I get the correct response back.**


Solution

  • I don't know what logic you are using but from my personnal experience, i have never seen sombedy put a @RequestParamin a Repository interface.
    This is how i would do it :

    1. You data classes are fine, nothing to note there
    2. The interface for extending JpaRepositoryis not the place for your @RequestParam. try to do it like this :
    @Repository    
    public interface VirusRepository extends JpaRepository<Virus, Long> {  
        List<Virus> findByRiskContainingIgnoreCase(String risk);    
    }
    
    1. Then create an interface than will map all your methods :
    public interface VirusService {
        List<Virus> findByrisk(String risk);    
    }
    
    1. Create a service implementation class to implement all your service methods :
    @Service
    public class VirusServiceImpl implements VirusService {    
        
        @Autowired
        private VirusRepository repository;   
    
        @Override
        public List<Virus> findByRisk(String risk) {   
            return repository.findByRiskContainingIgnoreCase(risk);   
        }    
    }
    
    1. You create the controller that will accept your request :
    @RestController    
    @RequestMapping("/virus/")    
    public class VirusController {    
         
         @Autowired
         private VirusService service;    
    
         @GetMapping("/findByRisk")
         public List<Virus> findByRisk(@RequestParam("risk") String risk) {    
             return service.findByRisk(risk);   
         }    
    }
    
    1. Make a request to you localhost:8080/risk/findByRiskand it should be fine.