When using map as a collection mapping, the key and map-key must be mapped as a column to make hibernate generate the right sql statement.
Bad Example :
Code:
<map lazy="false" cascade="all,delete-orphan" name="priceTiers" table="TPRODUCTPRICETIER" order-by="MIN_QUANTITY asc">
<key column="PRODUCT_PRICE_UID"/>
<map-key type="java.lang.Integer" formula="MIN_QUANTITY" />
<composite-element class="com.elasticpath.domain.catalog.impl.PriceTierImpl">
<property name="listPrice" type="java.math.BigDecimal" column="LIST_PRICE"/>
<property name="salePrice" type="java.math.BigDecimal" column="SALE_PRICE"/>
<property name="minQty" type="int" column="MIN_QUANTITY"/>
</composite-element>
</map>
The update sql statement hibernate created for the bad example:
Code:
update TPRODUCTPRICETIER set LIST_PRICE=?, SALE_PRICE=?, MIN_QUANTITY=? where PRODUCT_PRICE_UID=? and LIST_PRICE=? and SALE_PRICE=? and MIN_QUANTITY=?
The above sql statement will fail if any column, like SALE_PRICE, has a null value in the db because "SALE_PRICE=null" doesn't work well.
Good Example(Notice the "map-key" line has been changed to "column" rather than "formula".):
Code:
<map lazy="false" cascade="all,delete-orphan" name="priceTiers" table="TPRODUCTPRICETIER" order-by="MIN_QUANTITY asc">
<key column="PRODUCT_PRICE_UID"/>
<map-key type="java.lang.Integer" column="MIN_QUANTITY" />
<composite-element class="com.elasticpath.domain.catalog.impl.PriceTierImpl">
<property name="listPrice" type="java.math.BigDecimal" column="LIST_PRICE"/>
<property name="salePrice" type="java.math.BigDecimal" column="SALE_PRICE"/>
<property name="minQty" type="int" formula="MIN_QUANTITY"/>
</composite-element>
</map>
The update sql statement hibernate created for the above good example:
Code:
update TPRODUCTPRICETIER set LIST_PRICE=?, SALE_PRICE=?, MIN_QUANTITY=? where PRODUCT_PRICE_UID=? and MIN_QUANTITY=?