Question

I have two tables, quotation and quotation_item. There is one to many relationship them. My requirement is when I do quotation.setQuotationItem(set), old quotationItem mapped to quotation should get removed and quotationItems in set should get mapped against quotation.

/*** My Quotation pojo is like ****/

    public class Quotation {
    int quotationId;
    String code;
    String clientName;
    Set<QuotationItem> quotationItem=new HashSet<QuotationItem>();
    //getter and setter
    }

/*** QuotationItem POJO ****/

    public class QuotationItem extends Quotation{
    int id;
    Quotation quotation;
    String itemName;
    int rate;
    int qty;
    //getter and 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 is like for Quotation

quotation_id code client_name

My table structure for Quotation_item

id quotation_id item_name rate qty

Was it helpful?

Solution

Try to modify the set rather than replace it. i.e.:

    quotation.getQuotationItem().clear();
    quotation.getQuotationItem().addAll(newSet);

This is due to some hibernate weirdness.

Aside, a small point about naming. Your Quotation.quotationItem should be named quotationItems as it contains more than one QuotationItem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top