I am trying to do an insert for return generated id INSERT RETURNING id. In postgres editor it work without problems, but in code execution - java 1.6 with iBatis 3 (8.4 postgres driver version 8.开发者_如何学Go4-702) gives the error - Caused by: org.postgresql.util.PSQLException: ERROR: syntax error at or near " RETURNING. " It this means Postgres does not support Returning to insert?
I found this online - Concurrency issues when retriveing Ids of newly inserted rows with ibatis
but not how to do
Code iBatis xml
<insert id="insertNewItem" parameterType="itemAlias" useGeneratedKeys="true" keyProperty="item_id">
INSERT INTO items (
category_id,
description,
...)
VALUES(
#{category_id},
#{description},
...)
RETURNING item_id
</insert>
La respuesta es:
Public void insert(Item itemAlias) {
SqlSession session = sqlSessionFactory.openSession();
try {
Session.getMapper(ItemMapper.class).insert(itemAlias);
Logger.debug("itemAlias id:" + itemAlias.getItem_id()); // Here this you give you the generated key.
}
Finally {
Session.close();
}}
MyBatis xml
<insert id="insertNewItem" parameterType="itemAlias" useGeneratedKeys="true" keyProperty="item_id">
INSERT INTO items (
category_id,
description,
...)
VALUES(
#{category_id},
#{description},
...)
</insert>
MyBatis guys helped me. Thanks a lot
i htink u miss some concept here. RETURNING is neither RETURN nor SELECT. it just throw some variable out. To make it work, the statement gonna look like this
declare tmp integer;
INSERT INTO items (
category_id,
description,
...)
VALUES(
#{category_id},
#{description},
...)
RETURNING item_id INTO tmp
return tmp;
u can change integer to your primary key data type
精彩评论