[英]OneToMany mapping not working in the Controller crud Spring Boot Rest API
I don't know how to solve the 500 status response by the api. 我不知道如何通过api解决500状态响应。 I am testing with Postman.
我正在与邮递员进行测试。
The request 要求
{
"nome": "Ciencia da Computação",
"totalCreditos": 2333,
"professor": {
"id": 2,
"matricula": 0,
"nome": "José da silva"
}
}
How do I implement One to many relationship and expose the api the correct way for CRUD operation. 如何实现一对多关系并以正确的方式公开API以进行CRUD操作。 I'm getting a 500 error.
我遇到500错误。 I don't know if I need to change the Controller functions
我不知道是否需要更改Controller功能
CursoController.java CursoController.java
@CrossOrigin(origins = "*")
@RestController
@RequestMapping({"/api/curso"})
public class CursoController {
@Autowired
private CursoService cursoService;
private ProfessorService professorService;
@PostMapping
public Curso create(@RequestBody Curso curso){
return cursoService.create(curso);
}
@GetMapping(path = {"/{id}"})
public Curso findOne(@PathVariable("id") int id){
return cursoService.findById(id);
}
@PutMapping
public Curso update(@RequestBody Curso Curso){
return cursoService.update(Curso);
}
@DeleteMapping(path ={"/{id}"})
public Curso delete(@PathVariable("id") int id) {
return cursoService.delete(id);
}
@GetMapping
public List findAll(){
return cursoService.findAll();
}
}
Curso.java Curso.java
@Entity
@Table(name = "curso")
public class Curso implements Serializable {
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
@GeneratedValue(strategy = GenerationType.AUTO)
private Long codigo;
@Column
private Long totalCreditos;
@Column
private String nome;
@ManyToOne(fetch = FetchType.LAZY, optional = false, cascade =
CascadeType.PERSIST)
@JoinColumn(name = "professor_id", nullable = false)
@OnDelete(action = OnDeleteAction.CASCADE)
@JsonIgnore
@JsonManagedReference
private Professor professor;
}
you should never use CascadeType.ALL on @ManyToOne since entity state transitions should propagate from Parent entities to Child ones. 您永远不要在@ManyToOne上使用CascadeType.ALL,因为实体状态转换应从父实体传播到子实体。
Remove 去掉
(fetch = FetchType.LAZY, optional = false, cascade =
CascadeType.PERSIST)
from 从
@ManyToOne(fetch = FetchType.LAZY, optional = false, cascade =
CascadeType.PERSIST)
and add it in 并添加到
@OneToMany(fetch = FetchType.LAZY, optional = false, cascade =
CascadeType.PERSIST)
side. 侧。
Explanation : 说明:
As vladmihalcea explained in this article and in his book, High-Performance Java Persistence , you should never use CascadeType.ALL
on @ManyToOne
since entity state transitions should propagate from Parent entities to Child ones. 正如vladmihalcea在他的文章和他的《 高性能Java持久性》一书中所解释的那样,您永远不要在
@ManyToOne
上使用CascadeType.ALL
,因为实体状态转换应该从父实体传播到子实体。
The @ManyToOne
side is always the Child association since it should map the underlying FK. @ManyToOne
端始终是Child关联,因为它应该映射基础FK。
Therefore, move the CascadeType.ALL
from the @ManyToOne
association to the @OneToMany
which should use the mappedBy
attribute since it's the most efficient one-to-many mapping . 因此,移动
CascadeType.ALL
从@ManyToOne
联想到@OneToMany
应该使用mappedBy
属性,因为它是最有效的一个一对多的映射 。 ` `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.