英文:
Spring boot using Postman returning null when making a POST with foreign key
问题
代码部分不要翻译,只返回翻译好的内容:
我正在使用POSTMAN,尝试使用Spring Boot进行POST请求。我在“paciente”和“Operativo”之间有一个一对一的关系。当我尝试使用Postman创建一个“Operativo”实例时,与该“Operativo”关联的所有“paciente”属性都为空。我不知道是模型中的关系问题,控制器问题还是其他什么问题。
“paciente”模型中的关系
@Entity
@Table(name = "Paciente")
public class paciente {
@Id
private long rut;
private String nombre;
private String nacionalidad;
private String sexo;
private String fecha_na;
private String domicilio;
private String diagnostico;
private String telefono;
private String gravedad;
@OneToOne(mappedBy = "paciente")
private Operativo operativo;
}
“Operativo”模型中的关系
@Entity
@Table(name = "Operativo")
public class Operativo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String dia;
private String hora;
private String equipamiento;
private String equipo;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "paciente", referencedColumnName = "rut")
public paciente paciente;
}
OperativoController
@RestController
@CrossOrigin
@RequestMapping("/api/Operativo")
public class OperativoController {
@Autowired
private operativoService operativoService;
@GetMapping("")
public Iterable<Operativo> getOperativos() {
return operativoService.listAll();
}
@PostMapping("")
public ResponseEntity<Operativo> addOperativo(@RequestBody Operativo operativo) {
Operativo ope = operativoService.saveOrUpdateOperativo(operativo);
return new ResponseEntity<Operativo>(ope, HttpStatus.CREATED);
}
}
operativoService
@Service
public class operativoService {
@Autowired
private OperativoRepository operativoRepository;
public Operativo saveOrUpdateOperativo(Operativo operativo) {
return operativoRepository.save(operativo);
}
public Iterable<Operativo> listAll() {
return operativoRepository.findAll();
}
public void deleteOperativo(int id) {
operativoRepository.deleteById(id);
}
public Operativo getOperativoById(int id) {
return operativoRepository.findById(id);
}
}
POSTMAN的POST输入
{
"id": 1,
"equipamiento": "o",
"equipo": "a",
"hora": "a",
"dia": "a",
"paciente": {
"rut": 123123
}
}
POSTMAN输出
{
"id": 1,
"dia": "a",
"hora": "a",
"equipamiento": "o",
"equipo": "a",
"paciente": {
"rut": 123123,
"nombre": null,
"nacionalidad": null,
"sexo": null,
"fecha_na": null,
"domicilio": null,
"diagnostico": null,
"telefono": null,
"gravedad": null
}
}
英文:
I'm using POSTMAN and trying to do a post request with Spring Boot. I have an OneToOne relationship between "paciente" and "Operativo". When i try to create an "Operativo" instance with Postman, all the "paciente" attributes asociated with that "Operativo" are null. I don't know if it's problem of the relationship in the models, the controller or something else.
Relation in "paciente" model
@Entity
@Table(name = "Paciente")
public class paciente {
@Id
private long rut;
private String nombre;
private String nacionalidad;
private String sexo;
private String fecha_na;
private String domicilio;
private String diagnostico;
private String telefono;
private String gravedad;
@OneToOne(mappedBy = "paciente")
private Operativo operativo;
Relation in "Operativo" model
@Entity
@Table(name = "Operativo")
public class Operativo{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
private String dia;
private String hora;
private String equipamiento;
private String equipo;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "paciente", referencedColumnName = "rut")
public paciente paciente;
OperativoController
@RestController
@CrossOrigin
@RequestMapping("/api/Operativo")
public class OperativoController{
@Autowired
private operativoService operativoService;
@GetMapping("")
public Iterable<Operativo> getOperativos(){
return operativoService.listAll();
}
@PostMapping("")
public ResponseEntity<Operativo> addOperativo (@RequestBody Operativo operativo){
Operativo ope= operativoService.saveOrUpdateOperativo(operativo);
return new ResponseEntity<Operativo>(ope,HttpStatus.CREATED);
}
}
operativoService
@Service
public class operativoService{
@Autowired
private OperativoRepository operativoRepository;
public Operativo saveOrUpdateOperativo(Operativo operativo){
return operativoRepository.save(operativo);
}
public Iterable<Operativo> listAll(){
return operativoRepository.findAll();
}
public void deleteOperativo(int id){
operativoRepository.deleteById(id);
}
public Operativo getOperativoById(int id){
return operativoRepository.findById(id);
}
}
POSTMAN post input
{
"id" : 1,
"equipamiento" : "o",
"equipo" : "a",
"hora":"a",
"dia": "a",
"paciente": {"rut":123123}
}
POSTMAN Output
{
"id": 1,
"dia": "a",
"hora": "a",
"equipamiento": "o",
"equipo": "a",
"paciente": {
"rut": 123123,
"nombre": null,
"nacionalidad": null,
"sexo": null,
"fecha_na": null,
"domicilio": null,
"diagnostico": null,
"telefono": null,
"gravedad": null
}
}
答案1
得分: 0
- 首先,在一对一的关系中,您不需要使用 "referencedColumnName"。
- 您的 Postman 输入应该提供给 "paciente"。在上面,我们只看到了一列数据。您需要为所有必要的值提供输入。
英文:
1.First thing you don't need referencedColumnName when we are with one to one relationship.
2.Your postman input to be given for "paciente". Above we are seeing only one column data. You have to give input for all neccesary values u need
答案2
得分: 0
Finally, I fixed my problem, changing this line of code on the "Operativo" relation
@JoinColumn(name = "paciente", referencedColumnName = "rut")
to
@JoinColumn(name = "rut")
It seems that the "name" attribute should match the name of the foreign key that I'm referencing, instead of the name of the entity referenced.
Now using this POSTMAN Input.
{"id": 1 ,
"dia":"asd",
"hora":"asd",
"equipamiento":"asd",
"equipo":"asd",
"paciente":{"rut":19}}
I get the following Output
{
"id": 1,
"dia": "asd",
"hora": "asd",
"equipamiento": "asd",
"equipo": "asd",
"paciente": {
"rut": 19,
"nombre": "asd",
"nacionalidad": "asd",
"sexo": "asd",
"fecha_na": "asd",
"domicilio": "asd",
"diagnostico": "asd",
"telefono": "asd",
"gravedad": "asd"
}
}
Also removed @OneToOne(cascade = CascadeType.ALL)
in the "Operativo" class, because it was causing that when I create an "Operativo", it also creates the "Paciente" entity with the given data. So, when creating an "Operativo", if I give it only {"rut":19}
in the JSON, it will create a "Paciente" with only that attribute and all the others will be null.
英文:
Finally i fixed my problem, changing this line of code on "Operativo" relation
@JoinColumn(name = "paciente", referencedColumnName = "rut")
to
@JoinColumn(name = "rut")
It seems that "name" attribute should match the name of the foreign key that i'm referencing, instead of the name of the entity referenced.
Now using this POSTMAN Input.
{"id":1 ,
"dia":"asd",
"hora":"asd",
"equipamiento":"asd",
"equipo":"asd",
"paciente":{"rut":19}}
I get the following Output
{
"id": 1,
"dia": "asd",
"hora": "asd",
"equipamiento": "asd",
"equipo": "asd",
"paciente": {
"rut": 19,
"nombre": "asd",
"nacionalidad": "asd",
"sexo": "asd",
"fecha_na": "asd",
"domicilio": "asd",
"diagnostico": "asd",
"telefono": "asd",
"gravedad": "asd"
}
}
Also removed @OneToOne(cascade = CascadeType.ALL)
in the "Operativo" class, because it was causing that when i create an "Operativo", it also creates the "Paciente" entity with the given data. So, when creating an "Operativo" if i give it only "paciente":{"rut":19}
in the JSON, it will create a "Paciente" with only that attribute and all the others will be null.
专注分享java语言的经验与见解,让所有开发者获益!
评论