2

Hello Stackoverflow, I had hard time with OneToMany mapping I have googled to find the solution from morning nothing helped.

I am trying to do @OneToMany mapping relationship. following are the classes:

class: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

class: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

I am using session.save() method in this manner

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 is generating the query in this manner

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

foreign key value is not inserting, in database table it is showing empty(null) I am unable to rectify where I am going wrong. Thank in advance.. Hope for best..

2 Answers2

1

I think the problem might be that you are setting the @JoinColumn insertable attribute to false, which means that the column will not be included in any INSERT statements. Also, if you do updateable=false, it will not be included in any UPDATE statements.

For more info, please refer to this article.

IntelliData
  • 441
  • 6
  • 29
0

Seems your setting updatable and insertable to false. It means to say they are read only. Please check below link Stack over flow Updatable and isnertable discussion

Community
  • 1
  • 1
Lokesha S
  • 75
  • 1
  • 4
  • 16