Hi all,
I run into some foreign key constraints problems when performing insert
commands with ibatis/spring during a transaction.
I try to do the folowing :
try {
>
> getSqlMapClient().startTransaction();
>
> Inventory inventory = new Inventory(campaignId, networkId, positionId,
> sectionId);
> getSqlMapClientTemplate().insert("insertInventory", inventory);
>
> inventoryData.setInventory(inventory);
> getSqlMapClientTemplate().insert("insertInventoryData", inventoryData);
>
> getSqlMapClient().commitTransaction();
>
> } finally {
>
> getSqlMapClient().endTransaction();
>
> }
My ibatis Inventory.xml map :
<sqlMap namespace="Inventory">
>
> <typeAlias alias="Inventory" type="domain.Inventory" />
>
> <typeAlias alias="InventoryData" type="domain.InventoryData" />
>
> <insert id="insertInventory" parameterClass="Inventory">
> INSERT INTO inventory (
> user_campaign_id,
> network_id,
> position_id,
> section_id,
> state_id
> )
> VALUES (
> #userCampaignId#,
> #networkId#,
> #positionId#,
> #sectionId#,
> 1
> );
> <selectKey resultClass="int" keyProperty="id">
> SELECT LAST_INSERT_ID() as id
> </selectKey>
> </insert>
>
> <insert id="insertInventoryData" parameterClass="InventoryData">
> INSERT INTO inventory_data (
> inventory_id,
> datetime,
> uu,
> imps,
> sov,
> cpm,
> cpm_gross,
> capping,
> capping_real,
> budget,
> budget_gross
> )
> VALUES (
> #inventory.id#,
> #date#,
> #uu#,
> #imps#,
> #sov#,
> #cpm#,
> #cpmGross#,
> #capping#,
> #cappingReal#,
> #budget#,
> #budgetGross#
> );
> </insert>
>
> </sqlMap>
When I run this, a MySQLIntegrityConstraintViolationException is thrown ,
telling me that a foreign key constraint fails on the ID of the Inventory (
inventory.id).
If I set <property name="defaultAutoCommit" value="true" /> then it works
...
Unfortunately I must only commit if the whole transaction passed.
Am I doing something wrong ?
If someone can help me, it would be great.
Thx
Toni Van de Voorde