java - Bidirectional mapping in hibernate -
i have 2 tables, quotation , quotation_item. there 1 many relationship them. requirement when quotation.setquotationitem(set), old quotationitem mapped quotation should removed , quotationitems in set should mapped against quotation.
/*** quotation pojo ****/
public class quotation { int quotationid; string code; string clientname; set<quotationitem> quotationitem=new hashset<quotationitem>(); //getter , setter } /*** quotationitem pojo ****/
public class quotationitem extends quotation{ int id; quotation quotation; string itemname; int rate; int qty; //getter , setter } /** quotation.hbm ********/
<hibernate-mapping> <class name="com.paramatrix.pojo.quotation" table="quotation" > <id name="quotationid" type="int" column="quotation_id"> <generator class="native" /> </id> <property name="code" column="code" type="string" /> <property name="clientname" column="client_name" type="string" /> <set name="quotationitem" table="quotation_item" cascade="save-update" inverse="true"> <key> <column name="quotation_id" not-null="true" /> </key> <one-to-many class="com.paramatrix.pojo.quotationitem" /> </set> </class> </hibernate-mapping> /** quotationitem.hbm ******/
<class name="com.paramatrix.pojo.quotationitem" table="quotation_item" dynamic-insert="true"> <id name="id" type="int" column="id"> <generator class="native" /> </id> <many-to-one name="quotation" class="com.paramatrix.pojo.quotation" > <column name="quotation_id" not-null="true" /> </many-to-one> <property name="itemname" column="item_name" type="string" /> <property name="rate" column="rate" type="int" /> <property name="qty" column="qty" type="int" /> </class> my tables structure quotation
quotation_id code client_name
my table structure quotation_item
id quotation_id item_name rate qty
try modify set rather replace it. i.e.:
quotation.getquotationitem().clear(); quotation.getquotationitem().addall(newset); this due hibernate weirdness.
aside, small point naming. quotation.quotationitem should named quotationitems contains more 1 quotationitem.
Comments
Post a Comment