2016-06-21 62 views
2

你好Stackoverflow,我很難與OneToMany映射我已經搜索了從早上找到解決方案沒有任何幫助。外鍵值在OneToMany映射中插入值爲空

我正在嘗試執行@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: 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 (?, ?, ?, ?, ?, ?) 

外鍵值沒有插入,在數據庫表中它顯示爲空(空) 我無法糾正我錯誤的地方。 在此先感謝.. 希望最好..

回答

0

我認爲這個問題可能是您正在設置@JoinColumn插入屬性,這意味着列將不包含在任何INSERT聲明。此外,如果你做可更新 =假,它不會被包含在任何更新陳述。

欲瞭解更多信息,請參閱this文章。

+0

感謝您的幫助,問題已解決...謝謝:) –

+0

如果由於其中一個問題解決了問題,您能接受嗎? :) – IntelliData