外键值是在OneToMany映射中将值插入为null

时间:2016-06-21 18:26:52

标签: java hibernate null annotations

Hello Stackoverflow,我很难用OneToMany映射我已经用Google搜索从早上找到解决方案没有任何帮助。

我正在尝试做@OneToMany映射关系。 以下是课程:

类:1

@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

类:2

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "byno")
private int buyingnumber;
@Column(name = "productnumber")
private Integer productNumber;
@Column(name = "productname")
private String productName;
@Column(name = "productprice")
private Double productPrice;
@Column(name = "productquantity")
private Double productQuantity;
@Column(name = "producttotal")
private Double productTotal;

@ManyToOne(cascade=CascadeType.ALL)
@JoinColumn(name="pbid", referencedColumnName="cbid",  insertable=false, updatable=false)
private CustomerBuyingProductsDetails cbpd;
//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);

}

Hibernate以这种方式生成查询

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 (?, ?, ?, ?, ?, ?)

外键值未插入,在数据库表中显示为空(null) 我无法纠正我出错的地方。 预先感谢.. 希望最好..

2 个答案:

答案 0 :(得分:0)

似乎您的设置可更新且可插入为false。这意味着它们是只读的。请查看以下链接 Stack over flow Updatable and isnertable discussion

答案 1 :(得分:0)

我认为问题可能是您将 @JoinColumn 可插入 属性设置为 false ,表示该列不会包含在任何 INSERT 语句中。此外,如果您执行 可更新 = false ,则不会将其包含在任何 UPDATE 语句中。

有关详细信息,请参阅this文章。