java - Not able to map fields using Hibernate Mapping -
i want establish 1 many relation between table vendor detail , product detail. 1 vendor can have multiple products. when inserting data table inserting 4 fields not mapping vendorid productdetail table , query generated this.
hibernate: insert productinfo (productcategory, productdetails, productprice, vendorid) values (?, ?, ?, ?) shuld map vendor id in table empty.
vendordetail.java
package com.cts.entity; import javax.persistence.*; @entity @table(name = "vendorinfo") public class vendordetails { @id @column private long vendorid; @onetomany private productdetails productdetail; @column private string vendorname; @column private string password; public string getvendorname() { return vendorname; } public void setvendorname(string vendorname) { vendorname = vendorname; } public long getvendorid() { return vendorid; } public void setvendorid(long vendorid) { vendorid = vendorid; } public string getpassword() { return password; } public void setpassword(string password) { password = password; } }
productdetails.java
package com.cts.entity; import javax.persistence.cascadetype; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.generatedvalue; import javax.persistence.generationtype; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.manytoone; import javax.persistence.onetoone; import javax.persistence.table; @entity@table(name = "productinfo") public class productdetails { @manytoone(cascade = cascadetype.all)@joincolumn(name = "vendorid") private vendordetails vendordetails; public productdetails() { } @id @generatedvalue(strategy = generationtype.identity) @column private int productid; @column private string productcategory; @column private string productdetails; @column private string productprice; public vendordetails getvendordetails() { return vendordetails; } public void setvendordetails(vendordetails vendordetails) { this.vendordetails = vendordetails; } public int getproductid() { return productid; } public void setproductid(int productid) { this.productid = productid; } public string getproductcategory() { return productcategory; } public void setproductcategory(string productcategory) { this.productcategory = productcategory; } public string getproductdetails() { return productdetails; } public void setproductdetails(string productdetails) { this.productdetails = productdetails; } public string getproductprice() { return productprice; } public void setproductprice(string productprice) { this.productprice = productprice; } }
dao class productdetaildaoimpl.java
package com.cts.dao; import org.hibernate.sessionfactory; import org.springframework.beans.factory.annotation.autowired; import org.springframework.stereotype.repository; import org.springframework.transaction.annotation.transactional; import com.cts.entity.productdetails; import com.cts.entity.to.productdetailsto; @repository public class productdetaildaoimpl implements productdetaildao { @autowired sessionfactory sessionfactory; @transactional public boolean saveproductinfo(productdetailsto productto) { system.out.println("m in registration dao"); system.out.println(productto.getproductcategory()); system.out.println(productto.getproductdetails()); system.out.println(productto.getproductid()); system.out.println(productto.getproductprice()); //getting productto data entity class productdetails prodet = productto.getentity(); system.out.println("value of product details is:" + prodet.getproductdetails()); sessionfactory.getcurrentsession().save(prodet); return false; } }
vendordetails has many productdetails need make 1 many annotation this:-
@onetomany(mappedby="vendordetails") //mappedby value declared //in productdetails class. private collection<productdetails> productdetail=new arraylist<productdetails>;
and create setter , getter of this.
now in productdetails class need annotate many 1 this:- @manytoone(cascade = cascadetype.all) @joincolumn(name = "vendorid") private vendordetails vendordetails;
then new column named 'vendorid' create in table 'productinfo' , since declare mappedby value="vendordetails" each vendor id insert.
Comments
Post a Comment