Sergey Morozov
Sergey Morozov

Reputation: 4608

How to return multiple generated columns after insert in MyBatis(Oracle)

I have table with primary key, which consist of several columns. There is batch insert with special Oracle hook - ignore_row_on_dupkey_index. That hook allow to ignore Unique Constraint Exception, duplicate records just get ignored, whereas non duplicate get successfully inserted. Using simple jdbc I could easily return primary key(consist of several columns) by code:

try(PreparedStatement st = connectio.preparedStatement("insert /* ignore_row_on_dupkey(Table_name, Constraint) */ into TABLE(c1, c2, c3) values(?,?,?)", new String [] {"c1", "c2"})) {
//Batch insert then get generated keys
}

Then I could analyze duplicates by iterating over returned keys.

I want to achieve this the same by MyBatis. I found Options annotation, which allows to do it by setting property useGeneratedKeys and keyColumn. The problem is I have complex primary key, whereas keyColumn has type String. Also I dont want to use SelectKey annotation.

So my question is can I return several columns value and how by MyBatis or not?

Thank you.

Upvotes: 0

Views: 1870

Answers (1)

keyColumn allows to specify multiple columns. Here's relevant piece of the documentation (note the last sentence):

keyColumn | (insert and update only) Sets the name of the column in the table with a generated key. This is only required in certain databases (like PostgreSQL) when the key column is not the first column in the table. Can be a comma separated list of columns names if multiple generated columns are expected.

And an example from mybatis tests:

<insert id="insertTable2WithGeneratedKeyXml" useGeneratedKeys="true"
    keyProperty="nameId,generatedName" keyColumn="ID,NAME_FRED">
  insert into table2 (name) values(#{name})
</insert>

Upvotes: 1

Related Questions