imen chaker
imen chaker

Reputation: 15

How to fill multiple tables using a single @postmapping in SpringBoot

I develop an application that allows to manage the candidates, the application contains two tables (candidate and techno) joined with a @ManyToMany join table, I'm looking for how to fill both tables with the same @PostMapping, as my code indicates. I'm using an Angular application witch send a candidat with all the informations and a table of techno (that the candidat have to select, he can not add a new techno). I would like to join the new candidat with some techno. This is what the controller will receive:

{prenom: "Pname", nom: "Name", pseudo: "Pnamename", ecole: "school", mail: "[email protected]", …}
ecole: "school"
mail: "[email protected]"
nom: "Name"
numTel: "0123456789"
prenom: "Pname"
pseudo: "Pnamename"
roleCible: "poste"
secteurActivites: "sector"
techno: Array(3)
0: "android"
1: "drupal"
2: "html"
length: 3
__proto__: Array(0)
typeContrat: "CDI"
villeRecherchee: "Paris"
__proto__: Object

1- Candidat.java

@Entity
@Table(name = "Candidats")
public class Candidat {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String nom;
    private String prenom;
    private String ecole;
    private String numTel;
    private String mail;
    private String pseudo;
    private String roleCible;
    private String typeContrat;
    private String villeRecherchee;

    @Temporal(TemporalType.DATE)
    private Date dateCurrent = new Date();

    @ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE })
    @JoinTable(name = "candidat_techno", joinColumns = { @JoinColumn(name = "candidat_id") }, 
      inverseJoinColumns = {
            @JoinColumn(name = "techno_id") })

    private Set<Techno> techno = new HashSet<>();

    public Candidat() {

    }
    @SuppressWarnings("unchecked")
    public Candidat(String nom, String prenom, String ecole, String numTel, String mail, String pseudo,
            String roleCible, String typeContrat, String villeRecherchee, List<Techno> techno, Date dateCurrent,) {
        super();
        this.nom = nom;
        this.prenom = prenom;
        this.ecole = ecole;
        this.numTel = numTel;
        this.mail = mail;
        this.pseudo = pseudo;
        this.roleCible = roleCible;
        this.typeContrat = typeContrat;
        this.villeRecherchee = villeRecherchee;
        this.techno = (Set<Techno>) techno;
        this.dateCurrent = new Date();  

     //getters ans setters

2- CandidatController

    @CrossOrigin(origins = "http://localhost:4200")
    @RestController
    @RequestMapping("/avatar")
    public class CandidatController {

        @Autowired
        CandidatDao candidatdao;    
        @Autowired
        TechnoDao technoDao;

    @PostMapping(value = "/add-candidat")
    public Candidat addCandidate(@RequestBody Candidat Candidat) {

        Candidat candidatAdded = candidatdao.save(Candidat);
        return candidatAdded;   
technodao.save(Candidat.getTechno());
    }

}

3- CandidatDAO

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

4-Techno.java

@Entity
@Table(name = "techno")
public class Techno {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;

    private String nomTechno;

    @ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.PERSIST, CascadeType.MERGE }, mappedBy = "techno")

    private Set<Candidat> candidat = new HashSet<Candidat>();

    public Techno() {

    }

    @SuppressWarnings("unchecked")
    public Techno(String nomTechno, Candidat candidat) {
        super();
        this.nomTechno = nomTechno;
        this.candidat = (Set<Candidat>) candidat;
    }

    public String getNomTechno() {
        return nomTechno;
    }

    public void setNomTechno(String nomTechno) {
        this.nomTechno = nomTechno;
    }

    @Override
    public String toString() {
        return "Techno [nomTechno=" + nomTechno + ", candidat=" + candidat + "]";
    }

   //getters ans setters

5- TechnoController

@CrossOrigin(origins = "http://localhost:4200")
@RestController
@RequestMapping("/avatar")
public class TechnoController {

    @Autowired
    TechnoDao technodao;

    @PostMapping(value = "/add-techno")
    public Techno addCandidate(@RequestBody Techno Techno) {

        Techno technoAdded = technodao.save(Techno);
        return technoAdded;
    }

}

6- TechnoDao

 @Repository
    public interface TechnoDao extends JpaRepository<Techno, String> {
    Techno save(Set<Techno> techno);
    }

for now I can fill both tables, but with two different post mapping. how to fill both tables (techno and candidate) at the same time with a single @post mapping ?? like this:

    {
    id: 1,
    nom: "smith",
    prenom: "john",
    ecole: "usa",
    numTel: "11111",
    mail: "j@smith",
    pseudo: "JS",
    roleCible: "usa",
    typeContrat: "usa",
    villeRecherchee: "paris",       
    dateCurrent: "2019-10-02",
    techno: [
        {
          id: 1,
          nomTechno: "springBoot"
        },
        {
         id: 2,
         nomTechno: "java"
         }
           ]
  }

Upvotes: 1

Views: 1229

Answers (1)

Sudip Bolakhe
Sudip Bolakhe

Reputation: 523

In your CandidateController, Add this:

@Autowired
TechnoDao technoDao;

Inside post mapping use this:

technoDao.save(candidat.getTechno());

This has to help you.

Upvotes: 1

Related Questions