0

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

Limon Monte
  • 52,539
  • 45
  • 182
  • 213
Bhushan
  • 1,489
  • 3
  • 27
  • 45
  • @Taylor If I put 3 quotationItem in a Set and then assign that set to Quotation.setQuotationItem(set). Next time when I put 2 different quotationItem in a Set & then assign that set to Quotation then these two new quotationItem are also get mapped with Quotation. BUT I WANT ONLY LAST TWO QUOTATION ITEM GET MAPPED WITH QUOTATION. – Bhushan Mar 13 '14 at 02:29

1 Answers1

0

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.

Taylor
  • 3,942
  • 2
  • 20
  • 33
  • You can take a look on my code from this link https://www.dropbox.com/s/grrkv6kj6ad3xmb/ParentChildHibernateDemo.rar – Bhushan Mar 13 '14 at 18:34
  • Hi @Taylor, ur suggestion worked fine but for audit log purpose i am using findDirty() method of hibernate interceptor. In that method, value of old set(quotationItem) and new set(quotationItem) is being same. If i remove ur logic, it show difference. Any work around ? – Bhushan Jul 17 '14 at 11:08
  • I'm not familiar with `findDirty() method of hibernate interceptor` can you update your question with the code? – Taylor Jul 17 '14 at 17:43