表格:患者 ( id, 姓名, id_status, ...) -> FK 到 pattient_statuspattient_status (id, 描述) -> 目標表我需要的只是在 pattient.class 中獲取 pattient_status.description,因為我的 GET 方法需要 JSON 返回上的此信息。代碼:@Entity@Table(name="cad_paciente")public class Paciente {... (other columns)@OneToOne @JoinColumn(insertable=false, updatable=false, name = "id_status_paciente", referencedColumnName = "id") private StatusPaciente status;public String getStatusPaciente(){ return status.getStatus(); }----@Entity@Table(name="cad_status_paciente")public class StatusPaciente { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name="ds_status") @Size(max=50) private String status;這正確列出了我的信息,但在 POST 方法上,JPA 正確保存但返回消息:Resolved [org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: (was java.lang.NullPointerException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.spin.spincare.model.Paciente["statusPaciente"])]我應該怎么辦?
2 回答

達令說
TA貢獻1821條經驗 獲得超6個贊
這是你的吸氣劑的問題:
public String getStatusPaciente() {
return status.getStatus();
}
在您的 POST 調用中,狀態為 null,因此當 Jackson 使用此 getter 生成 JSON 時,它會出現空指針異常。將其更新為:
public String getStatusPaciente() {
if (status == null) {
return null;
}
return status.getStatus();
}

暮色呼如
TA貢獻1853條經驗 獲得超9個贊
使用@MapsId
.?這將使實體的 id 匹配。
@Entity
@Table(name="cad_status_paciente")
public class StatusPaciente {
? ? @Id
? ? @GeneratedValue(strategy = GenerationType.IDENTITY)
? ? private Long id;
? ? @MapsId
? ? @OneToOne
? ? private Paciente paciente;
? ? @Column(name="ds_status")
? ? @Size(max=50)
? ? private String status;
}
添加回答
舉報
0/150
提交
取消