Search code examples
javaspring-bootspring-data-jpaspring-datajpql

how to find the data by an attribute (name_techno) in jpql


I am developing an application that allows managing candidates in a company, for that I use spring-boot, in order to select the employees who master such a technology (Techno) I used a request JPQL.

So, How can I find a candidate by techno?

In my project I used this code:

1 - the class candidat.java

@Entity
public class Candidat {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "candidat_id")

    private int id;

    private String nom;
    private String prenom;
    private String ville;
    private int numTel;
    private String mail;
    private String pseudo;
    private String roleCible;
    private String typeContrat;
    private String villeRecherchee;

    @OneToMany(mappedBy="candidat")
    private List<Techno> techno;

    @Temporal(TemporalType.DATE)
    private Date date;

    @OneToMany
    private List<SecteurActivites> secteurActivites;

    public Candidat() {
        // TODO Auto-generated constructor stub
    }

2- the class Techno.java

@Entity
public class Techno {
    @Id
    @GeneratedValue
    @Column(name = "techno_id")
    private int id ;

    private String nomTechno;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "candidat_id", nullable = false)
    @OnDelete(action = OnDeleteAction.CASCADE)
    @JsonIgnore
    private Candidat candidat;
    public Techno() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @param nomTechno
     * @param candidat
     */
    public Techno(String nomTechno, Candidat candidat) {
        super();
        this.nomTechno = nomTechno;
        this.candidat = candidat;
    }

3- My CandidatController

@GetMapping(value = "/GetAllCandidats/{nomTechno}")
public List<Candidat> afficherCandidat(@PathVariable ("nomTechno")  String nomTechno){

     return  candidatdao.findByTechno(nomTechno);
}

4- the repository:

@Repository
public interface CandidatDao extends JpaRepository <Candidat, String>{

    List<Candidat> findByDate(Date date);

    @Query("SELECT DISTINCT e FROM Candidat e INNER JOIN e.Techno t")
    List<Candidat> findByTechno(String nomTechno);
    }

5- app.properties

server.port= 9090
spring.jpa.show-sql = true
spring.datasource.url= jdbc:mysql://localhost:3306/database
spring.datasource.username=??
spring.datasource.password=??
spring.jpa.hibernate.ddl-auto=update

The result in console is:

"Validation failed for query for method public abstract java.util.List com.avatar.dao.CandidatDao.findByTechno(java.lang.String)!"

Solution

  • You can declare the following method into your JpaRepository (also remove the @Query, it is not needed).

    List<Candidat> findDistinctByTechnoNomTechno(String nomTechno);
    

    Also in Techno.java you should add the @Column annotation and map it with the DB schema.

    I am not sure if you have pasted incomplete code of your entities on purpose. If not your entities are not correct. You should create setters/getters as the following

     private String nomTechno;
    
    @Column(name = "NOM_TECHNO")
    public String getNomTechno() {
        return nomTechno;
    }
    
    public void setNomTechno(String nomTechno){
       this.nomTechno = nomTechno;
    }
    

    Do the above for all variables in your entities.