当我添加新的子记录时,我有一个垃圾值,它开始出现在FK列中。以下是我的类和数据库值。 我不确定我是否将任何JPA或hibernate注释错误或者是代码问题。
父类:
@Component
@Entity
@NamedQuery(name = "Product.findAll", query = "SELECT p FROM Product p")
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private int parentRef;
private BigDecimal price;
private int prod_ID;
private String prodType;
private String remarks;
private int supplier_ID;
private String UOM;
public Product() {
}
@Column(name = "Name")
public String getName() {
return this.name;
}
@Column(name = "ParentRef")
public int getParentRef() {
return this.parentRef;
}
@Column(name = "Price")
public BigDecimal getPrice() {
return this.price;
}
@Id
@Column(name = "Prod_ID")
public int getProd_ID() {
return this.prod_ID;
}
@Column(name = "ProdType")
public String getProdType() {
return this.prodType;
}
@Column(name = "Remarks")
public String getRemarks() {
return this.remarks;
}
@Column(name = "Supplier_ID")
public int getSupplier_ID() {
return this.supplier_ID;
}
@Column(name = "UOM")
public String getUOM() {
return this.UOM;
}
public void setName(String name) {
this.name = name;
}
public void setParentRef(int parentRef) {
this.parentRef = parentRef;
}
public void setPrice(BigDecimal price) {
this.price = price;
}
public void setProd_ID(int prod_ID) {
this.prod_ID = prod_ID;
}
public void setProdType(String prodType) {
this.prodType = prodType;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public void setSupplier_ID(int supplier_ID) {
this.supplier_ID = supplier_ID;
}
public void setUOM(String UOM) {
this.UOM = UOM;
}
@Override
public String toString() {
return "Name = "+ this.getName() +"\n ParentRef="+this.getParentRef() +"\n Prod ID="+this.getProd_ID() +"\n ProdType= "+getProdType() +"\n Remarks= "+getRemarks()+"\n Supplier_ID="+getSupplier_ID()+"\n UOM = "+getUOM()+"\nPrice ="+getPrice();
}
}
儿童班:
@Entity(name="Productdetail")
public class ProductDetail implements Serializable {
private static final long serialVersionUID = 1L;
@Column(nullable=true , name = "Color")
private String Color;
@Column(nullable=true , name = "Designno")
private String DesignNo;
@Column(nullable=true , name = "Imgpath")
private String ImgPath;
@Column(nullable=true , name = "IsScrap")
private Integer IsScrap;
private Product Prod_ID;
@Id
@TableGenerator(
name="ProdDetailID",
table="ID_GEN",
pkColumnName="GEN_KEY",
valueColumnName="GEN_VALUE",
pkColumnValue="ProdDetailID",
allocationSize=1)
@GeneratedValue(strategy=GenerationType.TABLE, generator="ProdDetailID")
@Column(name = "Proddetail_ID")
private Integer ProdDetail_ID;
@Column(nullable=true , name = "Remarks")
private String Remarks;
public ProductDetail() {
}
public ProductDetail(String remarks, String imgPath, String designNo, String color, Integer isScrap,
Integer prodDetail_ID) {
super();
Remarks = remarks;
ImgPath = imgPath;
DesignNo = designNo;
Color = color;
IsScrap = isScrap;
//Prod_ID = prod_ID;
ProdDetail_ID = prodDetail_ID;
}
public String getColor() {
return Color;
}
public String getDesignNo() {
return DesignNo;
}
public String getImgPath() {
return ImgPath;
}
public Integer getIsScrap() {
return IsScrap;
}
@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="Prod_ID" )
public Product getProd_ID() {
return Prod_ID;
}
public Integer getProdDetail_ID() {
return ProdDetail_ID;
}
public String getRemarks() {
return Remarks;
}
public void setColor(String color) {
Color = color;
}
public void setDesignNo(String designNo) {
DesignNo = designNo;
}
public void setImgPath(String imgPath) {
ImgPath = imgPath;
}
public void setIsScrap(Integer isScrap) {
IsScrap = isScrap;
}
public void setProd_ID(Product prod_ID) {
Prod_ID = prod_ID;
}
public void setProdDetail_ID(Integer prodDetail_ID) {
ProdDetail_ID = prodDetail_ID;
}
public void setRemarks(String remarks) {
Remarks = remarks;
}
}
控制器:
@PostMapping(value="/save")
private void SaveData(@ModelAttribute ProductDetail productDetail ,@RequestParam("Product") int productID) {
productDetail.setProd_ID( getProductList().get(productID) );
System.out.println(productDetail.getProd_ID());
prodDetailService.save(productDetail);
}
@GetMapping(value = "/NewOrder")
public String NewProduct(Model model) {
model.addAttribute("product", new Product());
model.addAttribute("productDetail", new ProductDetail());
model.addAttribute("order", new Order());
model.addAttribute("orderDetial", new OrderDetail());
model.addAttribute("productList", getProductList());
return "NewOrder"; // Returns page named mentioned
}
protected List<Product> getProductList() {
productlist = new ArrayList<Product>();
productlist = ProdService.findAll();;
return productlist;
}
子表的数据库值是: Prod_ID包含一些未知值。
ProdDetail_ID Prod_ID Color DesignNo ImgPath IsScrap Remarks
------------- ---------- ----- --------- ------- ------- -------
3 **1946157361** Red design NULL NULL NULL
4 **1946157361** Red design NULL NULL NULL
父表是:
Prod_ID Name Price UOM ProdType ParentRef Supplier_ID Remarks
------- ---------- ----- ----- -------- --------- ----------- -------
1 Lawn NULL pcs 2 pcs 1 1 1
2 TestData 2500 Meter 3 0 1 Remarks
请帮我解决这个问题。