Skip to content

Commit df0f135

Browse files
committed
Fix for @options annotation and generated keys
1 parent d693245 commit df0f135

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
lines changed

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,15 +231,11 @@ private void parseStatement(Method method) {
231231
keyGenerator = handleSelectKeyAnnotation(selectKey, mappedStatementId, getParameterType(method));
232232
keyProperty = selectKey.keyProperty();
233233
} else {
234-
if (configuration.isUseGeneratedKeys()) {
235-
if (options == null) {
236-
keyGenerator = new Jdbc3KeyGenerator();
237-
} else {
238-
keyProperty = options.keyProperty();
239-
keyGenerator = options.useGeneratedKeys() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
240-
}
234+
if (options == null) {
235+
keyGenerator = configuration.isUseGeneratedKeys() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
241236
} else {
242-
keyGenerator = new NoKeyGenerator();
237+
keyGenerator = options.useGeneratedKeys() ? new Jdbc3KeyGenerator() : new NoKeyGenerator();
238+
keyProperty = options.keyProperty();
243239
}
244240
}
245241
} else {

src/test/java/org/apache/ibatis/submitted/selectkey/AnnotatedMapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.apache.ibatis.annotations.Insert;
44
import org.apache.ibatis.annotations.InsertProvider;
5+
import org.apache.ibatis.annotations.Options;
56
import org.apache.ibatis.annotations.SelectKey;
67

78
public interface AnnotatedMapper {
@@ -10,6 +11,10 @@ public interface AnnotatedMapper {
1011
@SelectKey(statement="call identity()", keyProperty="nameId", before=false, resultType=int.class)
1112
int insertTable2(Name name);
1213

14+
@Insert("insert into table2 (name) values(#{name})")
15+
@Options(useGeneratedKeys=true, keyProperty="nameId")
16+
int insertTable2WithOptions(Name name);
17+
1318
@Insert("insert into table3 (id, name) values(#{nameId}, #{name})")
1419
@SelectKey(statement="call next value for TestSequence", keyProperty="nameId", before=true, resultType=int.class)
1520
int insertTable3(Name name);

src/test/java/org/apache/ibatis/submitted/selectkey/SelectKeyTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ public void testAnnotatedInsertTable2() {
109109
}
110110
}
111111

112+
@Test
113+
public void testAnnotatedInsertTable2WithOptions() {
114+
SqlSession sqlSession = sqlSessionFactory.openSession();
115+
116+
try {
117+
Name name = new Name();
118+
name.setName("barney");
119+
AnnotatedMapper mapper = sqlSession.getMapper(AnnotatedMapper.class);
120+
int rows = mapper.insertTable2WithOptions(name);
121+
assertEquals(1, rows);
122+
assertEquals(22, name.getNameId());
123+
} finally {
124+
sqlSession.close();
125+
}
126+
}
127+
112128
@Test
113129
public void testAnnotatedInsertTable3() {
114130
SqlSession sqlSession = sqlSessionFactory.openSession();

0 commit comments

Comments
 (0)