我尝试通过发布JSON插入一些数据,但我不知道为什么,它返回一条错误消息,提示字段“ id_accessoire”不能为空(空)。
当我通过Advanced Rest Client发送JSON时,字段id_accessoire不为空。
首先,这是我的实体(文件Affaire.java),所有字段都在我的数据库中:
@Entity
@Data
@Table(name = "affaire")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
public class Affaire implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", insertable = false, nullable = false)
private Integer id;
@Column(name = "sn", nullable = false)
private String sn;
@Column(name = "zc")
private String zc;
@Column(name = "certificat")
private String certificat;
@Column(name = "etat", nullable = false)
private Boolean etat;
@Column(name = "finEval", nullable = false)
private Date finEval;
@Column(name = "finRep", nullable = false)
private Date finRep;
@Column(name = "limiteCalendaire", nullable = false)
private String limiteCalendaire;
@Column(name = "date_lancement", nullable = false)
private Date dateLancement;
@Column(name = "accordClient", nullable = false)
private boolean accordClient;
@Column(name="idAccessoire", insertable = false, updatable = false)
private Integer idAccessoire;
@Column(name="idFlux", insertable = false, nullable = false, updatable = false)
private Integer idFlux;
@Column(name="idReparation", insertable = false, nullable = false, updatable = false)
private Integer idReparation;
@Column(name="idVariante", insertable = false, nullable = false, updatable = false)
private Integer idVariante;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="idAccessoire", nullable = false)
@JsonBackReference(value = "affaireAccess")
private Accessoire accessoire;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="idFlux", nullable = false)
@JsonBackReference(value = "affaireFlux")
private Flux flux;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="idReparation", nullable = false)
@JsonBackReference(value = "affaireRep")
private Reparation reparation;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="idVariante", nullable = false)
@JsonBackReference(value = "affaireVariante")
private Variante variante;
@OneToMany(mappedBy = "affaire", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
//@JsonManagedReference(value = "affecterAffaire")
@ToString.Exclude
private List<Affecter> affecters;
}
最后,这是我尝试发布的JSON,但不起作用:
{
"sn":"4444AAA",
"zc":null,
"certificat":null,
"etat" : false,
"finEval": "2019-08-25",
"finRep": "2019-09-14",
"limiteCalendaire":"10R28",
"dateLancement":"2019-07-19",
"accordClient" : false,
"idAccessoire" : 1,
"idFlux": 1,
"idReparation": 2,
"idVariante" : 7,
}
错误是字段“ id_accessoire_不能为空(空)
答案 0 :(得分:0)
从您的实体类中,我可以看到idAccessoire列中的不可插入内容不可更新。
@Column(name="idAccessoire", insertable = false, updatable = false)
private Integer idAccessoire;
这意味着无论您在代码中设置哪个值,该值都将被忽略。
现在,如果在表配置中将idAccessoire定义为不可为空的字段,则会引发此错误。
1)您是否真的希望此字段不可插入或不可更新?
2)如果必须在数据库中将此列指定为非null,请尝试指定默认值(如果它适合您的业务逻辑)。