Spring Boot使用Postman进行POST时返回空值,该POST带有外键。

huangapple 未分类评论50阅读模式
英文:

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 = &quot;Paciente&quot;)
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 = &quot;paciente&quot;)
    private Operativo operativo;
   

Relation in "Operativo" model

@Entity
@Table(name = &quot;Operativo&quot;)
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 = &quot;paciente&quot;, referencedColumnName  = &quot;rut&quot;)

    public paciente paciente;

OperativoController

@RestController
@CrossOrigin
@RequestMapping(&quot;/api/Operativo&quot;)

public class OperativoController{
    @Autowired
    private operativoService operativoService;
    @GetMapping(&quot;&quot;)
    public Iterable&lt;Operativo&gt; getOperativos(){
        return operativoService.listAll();
    }
    @PostMapping(&quot;&quot;)
    public ResponseEntity&lt;Operativo&gt; addOperativo (@RequestBody Operativo operativo){
        Operativo ope= operativoService.saveOrUpdateOperativo(operativo);
        return new ResponseEntity&lt;Operativo&gt;(ope,HttpStatus.CREATED);
    }
    
}

operativoService

@Service
public class operativoService{
    @Autowired
    private OperativoRepository operativoRepository;
    public Operativo saveOrUpdateOperativo(Operativo operativo){
        return operativoRepository.save(operativo);
    }
    public Iterable&lt;Operativo&gt; listAll(){
        return operativoRepository.findAll();
    }
    public  void deleteOperativo(int id){
        operativoRepository.deleteById(id);
    }
    public Operativo getOperativoById(int id){
        return operativoRepository.findById(id);
    }
    
}

POSTMAN post input

{
    &quot;id&quot; : 1,
    &quot;equipamiento&quot; : &quot;o&quot;,
    &quot;equipo&quot; : &quot;a&quot;,
    &quot;hora&quot;:&quot;a&quot;,
    &quot;dia&quot;: &quot;a&quot;,
    &quot;paciente&quot;: {&quot;rut&quot;:123123}
}

POSTMAN Output

{
    &quot;id&quot;: 1,
    &quot;dia&quot;: &quot;a&quot;,
    &quot;hora&quot;: &quot;a&quot;,
    &quot;equipamiento&quot;: &quot;o&quot;,
    &quot;equipo&quot;: &quot;a&quot;,
    &quot;paciente&quot;: {
        &quot;rut&quot;: 123123,
        &quot;nombre&quot;: null,
        &quot;nacionalidad&quot;: null,
        &quot;sexo&quot;: null,
        &quot;fecha_na&quot;: null,
        &quot;domicilio&quot;: null,
        &quot;diagnostico&quot;: null,
        &quot;telefono&quot;: null,
        &quot;gravedad&quot;: null
    }
}

答案1

得分: 0

  1. 首先,在一对一的关系中,您不需要使用 "referencedColumnName"。
  2. 您的 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 = &quot;paciente&quot;, referencedColumnName  = &quot;rut&quot;)

to

@JoinColumn(name = &quot;rut&quot;)

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.

{&quot;id&quot;:1 ,
&quot;dia&quot;:&quot;asd&quot;,
&quot;hora&quot;:&quot;asd&quot;,
&quot;equipamiento&quot;:&quot;asd&quot;,
&quot;equipo&quot;:&quot;asd&quot;,
&quot;paciente&quot;:{&quot;rut&quot;:19}}

I get the following Output

{
    &quot;id&quot;: 1,
    &quot;dia&quot;: &quot;asd&quot;,
    &quot;hora&quot;: &quot;asd&quot;,
    &quot;equipamiento&quot;: &quot;asd&quot;,
    &quot;equipo&quot;: &quot;asd&quot;,
    &quot;paciente&quot;: {
        &quot;rut&quot;: 19,
        &quot;nombre&quot;: &quot;asd&quot;,
        &quot;nacionalidad&quot;: &quot;asd&quot;,
        &quot;sexo&quot;: &quot;asd&quot;,
        &quot;fecha_na&quot;: &quot;asd&quot;,
        &quot;domicilio&quot;: &quot;asd&quot;,
        &quot;diagnostico&quot;: &quot;asd&quot;,
        &quot;telefono&quot;: &quot;asd&quot;,
        &quot;gravedad&quot;: &quot;asd&quot;
    }
}

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 &quot;paciente&quot;:{&quot;rut&quot;:19} in the JSON, it will create a "Paciente" with only that attribute and all the others will be null.

huangapple
  • 本文由 发表于 2020年7月26日 11:39:43
  • 转载请务必保留本文链接:https://java.coder-hub.com/63095847.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定