I didn't see any examples or test cases of deleting rows in child
tables before the parent row is deleted. Suppose I have a stored
procedure with this code inside that I want to take out of the stored
procedure and convert into a SQL Map.

BEGIN TRAN
        DELETE FROM Baz WHERE FooId = #Id# -- child
        DELETE FROM Bar WHERE FooId = #Id# -- child
        DELETE FROM Foo WHERE Id = #Id# -- parent
COMMIT TRANS

I broke the statements into 3 seperate SQL Map entries:

<delete id="FooDelete_1_of_3" parameterClass="Foo">
        DELETE FROM Baz WHERE FooId = #Id#
</delete>
<delete id="FooDelete_2_of_3" parameterClass="Foo">
        DELETE FROM Bar WHERE FooId = #Id#
</delete>
<delete id="FooDelete_3_of_3" parameterClass="Foo">
        DELETE FROM Foo WHERE Id = #Id#
</delete>

SqlMapper sqlMap = base.GetLocalSqlMap();

try 
{
        sqlMap.BeginTransaction();
        sqlMap.Delete("FooDelete_1_of_3", foo);
        sqlMap.Delete("FooDelete_2_of_3", foo);
        sqlMap.Delete("FooDelete_3_of_3", foo);
        sqlMap.CommitTransaction();
} 
catch (Exception e) 
{
        sqlMap.RollBackTransaction();
        throw new IBatisNetException("Error executing query 'FooDelete' for
delete.  Cause: " + e.Message, e);
}

Is it better to have a single delete entry in the sql map?

<delete id="FooDelete" parameterClass="Foo">
        DELETE FROM Baz WHERE FooId = #Id#
        DELETE FROM Bar WHERE FooId = #Id#
        DELETE FROM Foo WHERE Id = #Id#
</delete>

- Ron

Reply via email to