我目前正在创建供应商表单,我们可以添加供应商并将其分配给不同的品牌和类别。这意味着供应商与supplier_has_brand_category 具有一对多关系。但我面临这个问题。前端供应商 obj 正确通过,没有任何其他问题。但后端supplier_has_brand_category为空。
供应商实体
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.*;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnore;
@Entity
@Table(name = "supplier")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SupplierEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id",unique = true)
private int id;
@Column(name = "supplierid",unique = true)
@NotNull
private String supplierid;
@Column(name = "name")
@NotNull
private String name;
@Column(name = "address")
private String Address;
@Column(name = "phone")
@NotNull
private String phone;
@Column(name = "email")
@NotNull
private String email;
@Column(name = "agentname")
@NotNull
private String agentname;
@Column(name = "agentphone")
@NotNull
private String agentphone;
@Column(name = "agentemail")
@NotNull
private String agentemail;
@Column(name = "branch")
@NotNull
private String branch;
@Column(name = "accountname")
@NotNull
private String accountname;
@Column(name = "accountno")
@NotNull
private String accountno;
@Column(name="addeddate")
@NotNull
private LocalDateTime addeddate;
@Column(name="modifydate")
private LocalDateTime modifydate;
@Column(name="deletedate")
private LocalDateTime deletedate;
@Column(name = "addeduser")
@NotNull
private int addeduser;
@Column(name = "modifyuser")
private int modifyuser;
@Column(name = "deleteuser")
private int deleteuser;
@ManyToOne
@JoinColumn(name="bankname_id",referencedColumnName = "id")
private BankNameEntity bankname_id;
@ManyToOne
@JoinColumn(name="supplierstatus_id",referencedColumnName = "id")
private SupplierStatusEntity supplierstatus_id;
@OneToMany(mappedBy = "supplier_id")
@JsonIgnore //block the recursion
private List<SupplierHasBrandCategoryEntity> supplier_has_brand_category;
}
供应商有品牌类别实体
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Table(name = "supplier_has_brand_category")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SupplierHasBrandCategoryEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id",unique = true)
private int id;
@ManyToOne
@JoinColumn(name="supplier_id",referencedColumnName = "id")
private SupplierEntity supplier_id;
@ManyToOne
@JoinColumn(name="brand_id",referencedColumnName = "id")
private BrandEntity brand_id;
@ManyToOne
@JoinColumn(name="category_id",referencedColumnName = "id")
private CategoryEntity category_id;
}
supplierController POST 请求
@PostMapping(value = "/supplier")
public String addSupplierData(@RequestBody SupplierEntity supplier){
//Authentication and Autherization
Authentication authentication=SecurityContextHolder.getContext().getAuthentication();
HashMap<String,Boolean> userPrivilage=privilageController.getPrivilageByUserModule(authentication.getName(),"SUPPLIER");
if(!userPrivilage.get("insert")){
return "Permission Denied! Save not Completed";
}
//check already existance
SupplierEntity extSupplierEmail=daoSupplier.getByEmail(supplier.getEmail());
if(extSupplierEmail!=null){
return "Save not Completed : given Name - "+supplier.getEmail()+" Already Exist...!";
}
try {
//set AutoGenarated Value
String nextNumber=daoSupplier.getNextSupplierNumber();
//if next employee number is not come then set manualy last number+1
if(nextNumber==null){
supplier.setSupplierid("SUP0001");
}else{
supplier.setSupplierid(nextNumber);
}
UserEntity addedUserData=daoUser.getByUsername(authentication.getName());
supplier.setAddeduser(addedUserData.getId());
supplier.setAddeddate(LocalDateTime.now());
/* System.out.println("Supplier data: " + supplier); */
for(SupplierHasBrandCategoryEntity supplierHasBrandCategory:supplier.getSupplier_has_brand_category()){
supplierHasBrandCategory.setSupplier_id(supplier);
}
daoSupplier.save(supplier);
return "OK";
} catch (Exception e) {
return "Save not Completed: "+e.getMessage();
}
}
}
当我单击“提交”按钮时,我控制台记录该对象。该对象具有“supplier_has_item_category”。但在后端它是空的。其他供应商数据打印正常。你能给我解决这个问题吗?
您应该跟踪supplier_has_brand_category为空时的情况,如下所示:
List<SupplierHasBrandCategoryEntity> supplier_has_brand_category = supplier.getSupplier_has_brand_category();
if (supplier_has_brand_category != null) {
for(SupplierHasBrandCategoryEntity supplierHasBrandCategory:supplier.getSupplier_has_brand_category()){
supplierHasBrandCategory.setSupplier_id(supplier);
}
}
但是,如果您使用 Jackson 功能并将 SupplyEntity 中的
@JsonIgnore
字段的 @JsonManagedReference
替换为 supplier_has_brand_category
,则完全可以避免在子对象中设置对父对象的引用:
@OneToMany(mappedBy = "supplier_id")
@JsonManagedReference
private List<SupplierHasBrandCategoryEntity> supplier_has_brand_category;
并为SupplierHasBrandCategoryEntity中的
@JsonBackReference
添加supplier_id field
:
@ManyToOne
@JsonBackReference
@JoinColumn(name="supplier_id", referencedColumnName = "id")
private SupplierEntity supplier_id;
最后,您的SupplierHasBrandCategoryEntity 子实体将不会持久,除非您仅保留SupplierEntity 父实体而不进行级联。
如果您不想自己保存子实体,可以为supplier_has_brand_category字段启用级联:
@OneToMany(mappedBy = "supplier_id", cascade = CascadeType.ALL)
@JsonManagedReference
private List<SupplierHasBrandCategoryEntity> supplier_has_brand_category;
然后你只需要保存你的父实体
daoSupplier.save(supplier)
这样Hibernate就会保存子实体。