[英]problem in handling one-to-many and many-to-one mapping
the problem is when i add sous_categorie for a categorie that already exists an exception appears问题是当我为已经存在的类别添加 sous_categorie 时出现异常
java.sql.SQLIntegrityConstraintViolationException: Le champ 'id_cat' ne peut être vide (null) java.sql.SQLIntegrityConstraintViolationException: Le champ 'id_cat' ne peut être vide (null)
i used postman for testing the API我用 postman 来测试 API
{
"id_Cat": "1274d819-ca54-455b-b69f-07a3f8b345c0",
"souCatName": "hospitals"
}
Class DBCategorie Class DB类别
@Entity
@Table(name = "categorie")
public class DBCategorie {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
private String idCat;
private String catName;
@OneToMany(mappedBy = "categorie")
private List<DBSousCategorie> souscategorie;
public DBCategorie() {
}
//getters& setter
public DBCategorie(String idCat, String catName, List<DBSousCategorie> souscategorie) {
super();
this.idCat = idCat;
this.catName = catName;
this.souscategorie = souscategorie;
}
public DBCategorie(String idCat, String catName) {
this.idCat = idCat;
this.catName = catName;
}
class DBSousCategorie class DBSous类别
@Entity
@Table(name = "souscategorie")
public class DBSousCategorie {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
private String idSouCat;
private String souCatName;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_Cat")
private DBCategorie categorie;
@OneToMany(mappedBy = "souscategorie")
private List<DBFile> files;
public DBSousCategorie() {
}
//Getters & Setters
public DBSousCategorie(String idSouCat, String souCatName, DBCategorie categorie) {
super();
this.idSouCat = idSouCat;
this.souCatName = souCatName;
this.categorie = categorie;
}
Did you set the parent categorie object to sous_category before persisting?您是否在坚持之前将父类别 object 设置为 sous_category?
dbSousCategory.setCategory(categorie);
subCategoryService.save(dbSousCategory);
And to avoid recursion use @JsonIgnore to ignore the parent category.为了避免递归,请使用 @JsonIgnore 忽略父类别。
@Entity
@Table(name = "souscategorie")
public class DBSousCategorie {
@Id
@GeneratedValue(generator = "uuid2")
@GenericGenerator(name = "uuid2", strategy = "uuid2")
private String idSouCat;
private String souCatName;
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="id_Cat")
@JsonIgnore
private DBCategorie categorie;
@OneToMany(mappedBy = "souscategorie")
private List<DBFile> files;
public DBSousCategorie() {
}
//Getters & Setters
public DBSousCategorie(String idSouCat, String souCatName, DBCategorie categorie) {
super();
this.idSouCat = idSouCat;
this.souCatName = souCatName;
this.categorie = categorie;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.