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?:
**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.**

I don't know what logic you are using but from my personnal experience, i have never seen sombedy put a
@RequestParamin aRepository interface.This is how i would do it :
JpaRepositoryis not the place for your@RequestParam. try to do it like this :interfacethan will map all your methods :controllerthat will accept your request :localhost:8080/risk/findByRiskand it should be fine.