gpt4 book ai didi

java - 如何同时填充多个表

转载 作者:行者123 更新时间:2023-12-01 22:37:41 28 4
gpt4 key购买 nike

我有一个带有 post 方法的 Spring Boot Rest Controller 。我想用相同的后映射同时填充两个表,因为知道两者具有多对多关系。

1-候选人

@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,)
{...}

2- CandidatController

@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

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

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

public Techno() {}

5-TechnoController

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);
}

现在我可以填充两个表,但使用两个不同的后映射。如何同时填写两个表(技术表和候选表)?像这样:

{
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"
}
]
}

最佳答案

据我所知,@RequestBody只能使用一次(即您只能将一个声明作为参数@RequestBody)。因此,您要么保留两个单独的映射,要么将这两个对象作为 JSON 作为 RequestBody 传递,然后在方法中使用它基于 JSON 数据“手动”创建这两个对象。

我在映射 here 中找到了关于多个 @RequestBody 的很好的解释.

希望这有帮助。

关于java - 如何同时填充多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58517615/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com