Java 外键值在OneToMany映射中插入值为null
你好,Stackoverflow,我很难使用OneToMany映射,从早上开始我就在谷歌上搜索以找到解决方案,但没有任何帮助 我正在尝试@OneToMany映射关系。 以下是课程: 班级:1Java 外键值在OneToMany映射中插入值为null,java,hibernate,null,annotations,Java,Hibernate,Null,Annotations,你好,Stackoverflow,我很难使用OneToMany映射,从早上开始我就在谷歌上搜索以找到解决方案,但没有任何帮助 我正在尝试@OneToMany映射关系。 以下是课程: 班级:1 @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "cbid") private int cbid; @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "ci
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "cbid")
private int cbid;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "cid", referencedColumnName = "custid", insertable = false, updatable = false)
private CustomerDetails customerDetails;
@Column(name = "customerid")
private Integer customerid;
@Column(name = "subtotal")
private Double subtotal;
@Column(name = "tax_amount")
private Double tax_amount;
@Column(name = "total_amount")
private Double total_amount;
@Column(name = "paid_amount")
private Double paid_amount;
@Column(name = "amount_due")
private Double amount_due;
@Column(name = "invoiceNumber")
private int invoiceNumber;
@Column(name = "d")
private Date d;
@OneToMany(mappedBy="cbpd", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
@OrderColumn(name="lindex")
private List<ProductNameBuyingDetails> pnbd = new ArrayList<ProductNameBuyingDetails>();
//setters and getters
我以这种方式使用session.save()方法
CustomerBuyingProductsDetails customerBuyingProductsDetails = new CustomerBuyingProductsDetails();
customerBuyingProductsDetails.setCustomerid(customerBuyingProducts.getCustomerid());
customerBuyingProductsDetails.setInvoiceNumber(customerBuyingProducts.getInvoiceNumber());
customerBuyingProductsDetails.setAmount_due(customerBuyingProducts.getAmount_due());
customerBuyingProductsDetails.setD(customerBuyingProducts.getD());
customerBuyingProductsDetails.setPaid_amount(customerBuyingProducts.getPaid_amount());
customerBuyingProductsDetails.setSubtotal(customerBuyingProducts.getSubtotal());
customerBuyingProductsDetails.setTax_amount(customerBuyingProducts.getTax_amount());
customerBuyingProductsDetails.setTotal_amount(customerBuyingProducts.getTotal_amount());
sessionFactory.getCurrentSession().save(customerBuyingProductsDetails);
Iterator<Integer> itrNum = customerBuyingProducts.getProductNumber().iterator();
Iterator<String> itrName = customerBuyingProducts.getProductName().iterator();
Iterator<Double> itrPrice = customerBuyingProducts.getProductPrice().iterator();
Iterator<Double> itrQuan = customerBuyingProducts.getProductQuantity().iterator();
Iterator<Double> itrTotal = customerBuyingProducts.getProductTotal().iterator();
int i=0;
while (itrNum.hasNext()) {
ProductNameBuyingDetails pnbd = new ProductNameBuyingDetails();
pnbd.setProductNumber(itrNum.next());
pnbd.setProductName(itrName.next());
pnbd.setProductPrice(itrPrice.next());
pnbd.setProductQuantity(itrQuan.next());
pnbd.setProductTotal(itrTotal.next());
pnbd.setCbpd(customerBuyingProductsDetails);
sessionFactory.getCurrentSession().save(pnbd);
customerBuyingProductsDetails.getPnbd().add(pnbd);
}
sessionFactory.getCurrentSession().save(customerBuyingProductsDetails);
}
外键值未插入,在数据库表中显示为空(null)
我无法纠正我的错误。
提前感谢。。
希望一切顺利。您的设置似乎是可更新的,可插入为false。这意味着它们是只读的。请查看下面的链接
我认为问题可能是您将@JoinColumn可插入的属性设置为false,这意味着该列将不会包含在任何插入语句中。此外,如果执行updateable=false,则它将不会包含在任何UPDATE语句中
有关更多信息,请参阅文章。谢谢:)您让我开心。如果问题因其中一个答案而得到解决,您能接受吗?:)
CustomerBuyingProductsDetails customerBuyingProductsDetails = new CustomerBuyingProductsDetails();
customerBuyingProductsDetails.setCustomerid(customerBuyingProducts.getCustomerid());
customerBuyingProductsDetails.setInvoiceNumber(customerBuyingProducts.getInvoiceNumber());
customerBuyingProductsDetails.setAmount_due(customerBuyingProducts.getAmount_due());
customerBuyingProductsDetails.setD(customerBuyingProducts.getD());
customerBuyingProductsDetails.setPaid_amount(customerBuyingProducts.getPaid_amount());
customerBuyingProductsDetails.setSubtotal(customerBuyingProducts.getSubtotal());
customerBuyingProductsDetails.setTax_amount(customerBuyingProducts.getTax_amount());
customerBuyingProductsDetails.setTotal_amount(customerBuyingProducts.getTotal_amount());
sessionFactory.getCurrentSession().save(customerBuyingProductsDetails);
Iterator<Integer> itrNum = customerBuyingProducts.getProductNumber().iterator();
Iterator<String> itrName = customerBuyingProducts.getProductName().iterator();
Iterator<Double> itrPrice = customerBuyingProducts.getProductPrice().iterator();
Iterator<Double> itrQuan = customerBuyingProducts.getProductQuantity().iterator();
Iterator<Double> itrTotal = customerBuyingProducts.getProductTotal().iterator();
int i=0;
while (itrNum.hasNext()) {
ProductNameBuyingDetails pnbd = new ProductNameBuyingDetails();
pnbd.setProductNumber(itrNum.next());
pnbd.setProductName(itrName.next());
pnbd.setProductPrice(itrPrice.next());
pnbd.setProductQuantity(itrQuan.next());
pnbd.setProductTotal(itrTotal.next());
pnbd.setCbpd(customerBuyingProductsDetails);
sessionFactory.getCurrentSession().save(pnbd);
customerBuyingProductsDetails.getPnbd().add(pnbd);
}
sessionFactory.getCurrentSession().save(customerBuyingProductsDetails);
}
Hibernate: insert into cb (amount_due, customerid, d, invoiceNumber, paid_amount, subtotal, tax_amount, total_amount, cbid) values (?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: insert into prodnamebuy (productname, productnumber, productprice, productquantity, producttotal, byno) values (?, ?, ?, ?, ?, ?)