How to make the insert with identity key setting work in SQL Server?
one should rely on "SELECT SCOPE_IDENTITY()" for that, but it does not work for me
public class TestMapper {
@Insert({ "insert into TEST (VAL_INT) values " +
"(#{valInt,jdbcType=INTEGER})" })
@SelectKey(statement = "SELECT SCOPE_IDENTITY()", keyProperty = "id", before = false, resultType = Integer.class)
int insert(Test record);
}
that seems to conclude that MyBatis executes the insert and the select in different batches (strange!), and that's
why SELECT SCOPE_IDENTITY() cannot work.
If that is so, then MyBatisGenerator is wrong in recommending that way, no? Or am I doing something wrong?
Has this ever worked?
BTW, I've use this approach with Postgresql (reading the sequence, instead of SELECT SCOPE_IDENTITY() , of course), without problems.