Greetings,
I really need help on this. I am trying to use Hibernate to insert on two table on the database. The relation is of type Parent/Child. The problem is that it inserts well on the Parent table but on the Child table it doesn't insert.
Debuging through the project I've found out that he makes an INSERT for the first table but for the second table it makes an UPDATE.
The hbm.xml files that I've built are bellow, the first file has lot of thing but what is used for these tables is at the end:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.retek.rpm.domain.promotion.bo.PromotionComponentDetailImpl"
table="RPM_PROMO_COMP_DETAIL" dynamic-update="true"
persister="com.retek.rpm.domain.core.dao.RetekNormalizedEntityPersister">
<composite-id name="objectId" class="com.retek.platform.bo.LongObjectId" access="field"
unsaved-value="any">
<key-property name="value" column="RPM_PROMO_COMP_DETAIL_ID" type="long"
access="field"/>
</composite-id>
<many-to-one name="promotionComponent"
class="com.retek.rpm.domain.promotion.bo.PromotionComponentImpl" column="PROMO_COMP_ID"
not-null="true" access="field"/>
<property name="displayId" column="PROMO_COMP_DETAIL_DISPLAY_ID" not-null="true"
type="string"/>
<property name="ignoreConstraintHandling" column="IGNORE_CONSTRAINTS"
type="boolean"/>
<property name="promotionApplyCode" column="APPLY_TO_CODE" not-null="true"
type="com.retek.rpm.domain.promotion.dao.PromotionApplyCodeConverter"/>
<property name="effectiveDateRange" access="field"
type="com.retek.rpm.domain.calendar.dao.DateRangeUserType">
<column name="START_DATE"/>
<column name="END_DATE"/>
</property>
<property name="createDate" not-null="true" column="CREATE_DATE"
type="com.retek.rpm.domain.calendar.dao.RDateType"/>
<property name="createID" not-null="true" column="CREATE_ID" type="string"/>
<property name="approvalDate" not-null="false" column="APPROVAL_DATE"
type="com.retek.rpm.domain.calendar.dao.RDateType"/>
<property name="approvalID" not-null="false" column="APPROVAL_ID" type="string"/>
<property name="stateCode" column="STATE" type="string"/>
<joined-subclass
name="com.retek.rpm.domain.promotion.bo.PromotionComponentSimpleDetailImpl"
table="RPM_PROMO_COMP_SIMPLE" dynamic-update="true"
persister="com.retek.rpm.domain.core.dao.RetekNormalizedEntityPersister">
<key column="RPM_PROMO_COMP_DETAIL_ID"/>
<property name="merchandiseNode"
type="com.retek.rpm.domain.promotion.dao.MerchandiseNodeUserType$All"
access="field">
<column name="MERCH_TYPE" not-null="true"/>
<column name="DEPT" not-null="false"/>
<column name="CLASS" not-null="false"/>
<column name="SUBCLASS" not-null="false"/>
<column name="ITEM" not-null="false"/>
<column name="DIFF_ID" not-null="false"/>
</property>
<property name="zoneGroupNode"
type="com.retek.rpm.domain.promotion.dao.ZoneGroupNodeUserType" access="field">
<column name="ZONE_NODE_TYPE"/>
<column name="ZONE_ID"/>
<column name="LOCATION"/>
</property>
<property name="retailChangeValue" access="field"
type="com.retek.rpm.domain.promotion.dao.RetailChangeValueUserType">
<column name="CHANGE_TYPE" not-null="true"/>
<column name="CHANGE_AMOUNT" not-null="false"/>
<column name="CHANGE_CURRENCY" not-null="false"/>
<column name="CHANGE_PERCENT" not-null="false"/>
<column name="CHANGE_SELLING_UOM" not-null="false"/>
<column name="TSL_VOUCHER_NUMBER" not-null="false"/>
<column name="TSL_VOUCHER_DESC" not-null="false"/>
</property>
<many-to-one name="priceGuide" column="PRICE_GUIDE_ID"
class="com.retek.rpm.domain.priceguide.bo.PriceGuideImpl" not-null="false"
access="field"/>
<property name="attribute1" column="ATTRIBUTE_1" type="string" access="field"/>
<property name="attribute2" column="ATTRIBUTE_2" type="string" access="field"/>
<property name="attribute3" column="ATTRIBUTE_3" type="string" access="field"/>
<many-to-one name="parentPromotionComponentDetail"
class="com.retek.rpm.domain.promotion.bo.PromotionComponentSimpleDetailImpl"
column="EXCEPTION_PARENT_ID" access="field"/>
</joined-subclass>
<joined-subclass
name="com.retek.rpm.domain.promotion.bo.PromotionComponentThresholdDetailImpl"
table="RPM_PROMO_COMP_THRESHOLD" dynamic-update="true"
persister="com.retek.rpm.domain.core.dao.RetekNormalizedEntityPersister">
<key column="RPM_PROMO_COMP_DETAIL_ID"/>
<property name="merchandiseNode"
type="com.retek.rpm.domain.promotion.dao.MerchandiseNodeUserType$All"
access="field">
<column name="MERCH_TYPE" not-null="true"/>
<column name="DEPT" not-null="false"/>
<column name="CLASS" not-null="false"/>
<column name="SUBCLASS" not-null="false"/>
<column name="ITEM" not-null="false"/>
<column name="DIFF_ID" not-null="false"/>
</property>
<property name="zoneGroupNode"
type="com.retek.rpm.domain.promotion.dao.ZoneGroupNodeUserType" access="field" >
<column name="ZONE_NODE_TYPE"/>
<column name="ZONE_ID"/>
<column name="LOCATION"/>
</property>
<many-to-one name="threshold" column="THRESHOLD_ID"
class="com.retek.rpm.domain.promotion.bo.ThresholdImpl" not-null="true"
access="field"/>
<property name="attribute1" column="ATTRIBUTE_1" type="string" access="field"/>
<property name="attribute2" column="ATTRIBUTE_2" type="string" access="field"/>
<property name="attribute3" column="ATTRIBUTE_3" type="string" access="field"/>
<property name="isExclusion" column="EXCLUSION" type="boolean" access="field"/>
<many-to-one name="parentPromotionComponentDetail"
class="com.retek.rpm.domain.promotion.bo.PromotionComponentThresholdDetailImpl"
column="EXCEPTION_PARENT_ID" access="field"/>
</joined-subclass>
<joined-subclass
name="com.retek.rpm.domain.promotion.bo.PromotionComponentBuyGetDetailImpl"
table="RPM_PROMO_COMP_BUY_GET" dynamic-update="true"
persister="com.retek.rpm.domain.core.dao.RetekNormalizedEntityPersister">
<meta attribute="lazy-load-collection">zoneGroupNodes</meta>
<key column="RPM_PROMO_COMP_DETAIL_ID"/>
<property name="all" column="BUY_ITEM_TYPE" type="boolean" not-null="true"/>
<property name="buyQuantity" column="BUY_ITEM_QTY"
type="com.retek.rpm.domain.promotion.dao.QuantityUserType" not-null="true"/>
<property name="retailChangeValue" access="field"
type="com.retek.rpm.domain.promotion.dao.RetailChangeValueUserType">
<column name="CHANGE_TYPE" not-null="true"/>
<column name="CHANGE_AMOUNT" not-null="false"/>
<column name="CHANGE_CURRENCY" not-null="false"/>
<column name="CHANGE_PERCENT" not-null="false"/>
<column name="CHANGE_SELLING_UOM" not-null="false"/>
<column name="TSL_VOUCHER_NUMBER" not-null="false"/>
<column name="TSL_VOUCHER_DESC" not-null="false"/>
</property>
<set name="buyItems" access="field" table="RPM_PROMO_BUY_ITEM" inverse="false"
lazy="true" cascade="none" persister="com.retek.rpm.domain.core.dao.RetekBasicCollectionPersister">
<key column="RPM_PROMO_COMP_DETAIL_ID" foreign-key="RPM_PROMO_COMP_DETAIL_ID"/>
<many-to-many class="com.retek.rpm.domain.core.bo.ItemImpl" column="ITEM"/>
</set>
<set name="getItems" access="field" table="RPM_PROMO_GET_ITEM" inverse="false"
lazy="true" cascade="none" persister="com.retek.rpm.domain.core.dao.RetekBasicCollectionPersister">
<key column="RPM_PROMO_COMP_DETAIL_ID" foreign-key="RPM_PROMO_COMP_DETAIL_ID"/>
<many-to-many class="com.retek.rpm.domain.core.bo.ItemImpl" column="ITEM"/>
</set>
<set name="zoneGroupNodes" access="field" table="RPM_PROMO_BUY_GET_ZONE"
inverse="false" lazy="false" cascade="none">
<key column="RPM_PROMO_COMP_DETAIL_ID"/>
<element type="com.retek.rpm.domain.promotion.dao.ZoneGroupNodeUserType">
<column name="ZONE_NODE_TYPE" sql-type="long"/>
<column name="ZONE_ID" sql-type="long"/>
<column name="LOCATION" sql-type="long"/>
</element>
</set>
<set name="promotionComponentDetailLocationExclusions" cascade="save-update"
access="field" inverse="false" lazy="true">
<key column="EXCLUSION_PARENT_ID"/>
<one-to-many
class="com.retek.rpm.domain.promotion.bo.PromotionComponentBuyGetDetailImpl"/>
</set>
<many-to-one name="parentPromotionComponentDetail"
class="com.retek.rpm.domain.promotion.bo.PromotionComponentBuyGetDetailImpl"
column="EXCLUSION_PARENT_ID" access="field"/>
</joined-subclass>
<!--Changes for Mod 93
############################################################
From this point is what matters for the question that I need answers. Ignore what is above.
############################################################
-->
<joined-subclass
name="com.retek.rpm.domain.promotion.bo.TslPromotionComponentMultiBuyDetailImpl"
table="TSL_RPM_PROMO_COMP_M_B" dynamic-update="true"
persister="com.retek.rpm.domain.core.dao.RetekNormalizedEntityPersister">
<meta attribute="lazy-load-collection">zoneGroupNodes</meta>
<key column="RPM_PROMO_COMP_DETAIL_ID"/>
<property name="multiBuyPromotionType" column="MULTI_BUY_PROMO_TYPE" type="com.retek.rpm.domain.core.dao.codeconverter.TslMultiBuyTypeStringCodeConverter" not-null="true"/>
<!-- coupon to be included -->
<set name="listDetails" inverse="true" lazy="true" cascade="save-update">
<key column="RPM_PROMO_COMP_DETAIL_ID" foreign-key="RPM_PROMO_COMP_DETAIL_ID"/>
<one-to-many class="com.retek.rpm.domain.promotion.bo.TslMultiBuyListImpl"/>
</set>
</joined-subclass>
</class>
</hibernate-mapping>
The precious hbm.xml file has a joined-class that works fine and has a set for a collection that is doing an update instead of an INSERT.
The class that is used in the set implements the following hbm.xml file.
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<!--
modified by: Lakshmi Natarajan - LN,lakshmi.natarajan@in.tesco.com
date: 26-April-2007 v1.0
Mod ref: 365a
Mod Description: Addition of new properties sTslBaseItem,bTslPriceMarkInd and bTslPrimPackInd corresponding to
ITEM_MASTER.TSL_BASE_ITEM,ITEM_MASTER.TSL_PRICE_MARK_IND and ITEM_MASTER.TSL_PRIM_PACK_IND
-->
<hibernate-mapping>
<class name="com.retek.rpm.domain.promotion.bo.TslMultiBuyListImpl" table="TSL_RPM_PROMO_COMP_M_B_DTL">
<id name="id" type="long" column="LIST_ID" unsaved-value="null">
<generator class="sequence">
<param name="sequence">TSL_MULTI_BUY_LIST_ID_SEQ</param>
</generator>
</id>
<many-to-one name="tslPromotionComponentMultiBuyDetailImpl" class="com.retek.rpm.domain.promotion.bo.TslPromotionComponentMultiBuyDetailImpl"
column="RPM_PROMO_COMP_DETAIL_ID" not-null="true" access="field" />
<!--property name="displayId" column="LIST_DISPLAY_ID" type="string" insert="false" update="false" access="field"/-->
<property name="listTypes" column="LIST_TYPE" type="com.retek.rpm.domain.core.dao.codeconverter.TslMultiBuyListTypeCodeConverter" insert="false" update="false" access="field"/>
<property name="buyListType" column="BUY_ITEM_TYPE" type="com.retek.rpm.domain.core.dao.codeconverter.TslMultiBuyPromotionBuyTypeCodeConverter" insert="false" update="false" access="field"/>
<property name="value" column="BUY_ITEM_VALUE" insert="false" update="false" access="field"/>
<property name="retailChangeValue" access="field"
type="com.retek.rpm.domain.promotion.dao.RetailChangeValueUserType" insert="false" update="false">
<column name="CHANGE_TYPE" not-null="false"/>
<column name="CHANGE_AMOUNT" not-null="false"/>
<column name="CHANGE_CURRENCY" not-null="false"/>
<column name="CHANGE_PERCENT" not-null="false"/>
<column name="CHANGE_SELLING_UOM" not-null="false"/>
<column name="VOUCHER_NUMBER" not-null="false"/>
<column name="VOUCHER_DESCRIPTION" not-null="false"/>
</property>
<property name="lockVersion" column="LOCK_VERSION" not-null="false" insert="false" update="false" access="field"/>
<property name="getQuantity" column="GET_QUANTITY" type="com.retek.rpm.domain.promotion.dao.QuantityUserType" not-null="false" insert="false" update="false" access="field"/>
</class>
</hibernate-mapping>
Here is the SQL statements that are produced by the hbm.xml files
Code:
insert into RPM_PROMO_COMP (COMP_DISPLAY_ID, NAME, CONSIGNMENT_RATE, STATE, SECONDARY_IND, PROMO_ID, DEAL_ID_ITEM, DEAL_DETAIL_ID_ITEM,
DEAL_ID_LOCATION, DEAL_DETAIL_ID_LOCATION, FUNDING_PERCENT, BUY_GET_UPTAKE_PERCENT, PROMO_COMP_ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
update TSL_RPM_PROMO_COMP_M_B_DTL set RPM_PROMO_COMP_DETAIL_ID=? where LIST_ID=?
I really need answers for:
How can I force the hbm.xml file to do an INSERT instead of an UPDATE?
Thanks for any reply
Many Thanks,
Rui FĂ©lix