Skip to content

Commit

Permalink
Merge pull request #782 from kazuki43zoo/Jdbc3KeyGenerator
Browse files Browse the repository at this point in the history
Raise a exception when keyProperty is not found
  • Loading branch information
emacarron authored Sep 19, 2016
2 parents 53a6aa6 + d7dfed9 commit 7164f15
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,14 @@ private TypeHandler<?>[] getTypeHandlers(TypeHandlerRegistry typeHandlerRegistry

private void populateKeys(ResultSet rs, MetaObject metaParam, String[] keyProperties, TypeHandler<?>[] typeHandlers) throws SQLException {
for (int i = 0; i < keyProperties.length; i++) {
String property = keyProperties[i];
if (!metaParam.hasSetter(property)) {
throw new ExecutorException("No setter found for the keyProperty '" + property + "' in " + metaParam.getOriginalObject().getClass().getName() + ".");
}
TypeHandler<?> th = typeHandlers[i];
if (th != null) {
Object value = th.getResult(rs, i + 1);
metaParam.setValue(keyProperties[i], value);
metaParam.setValue(property, value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@
public interface CountryMapper {

int insertList(List<Country> countries);
int insertUndefineKeyProperty(Country country);

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@
(#{country.countryname},#{country.countrycode})
</foreach>
</insert>
<insert id="insertUndefineKeyProperty" parameterType="org.apache.ibatis.submitted.keygen.Country" useGeneratedKeys="true" keyProperty="country_id">
insert into country (countryname,countrycode) values (#{countryname},#{countrycode})
</insert>
</mapper>
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,20 @@
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.executor.ExecutorException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.jdbc.ScriptRunner;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.hamcrest.CoreMatchers;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.hamcrest.CoreMatchers.*;

/**
* @author liuzh
Expand All @@ -37,6 +44,9 @@ public class Jdbc3KeyGeneratorTest {

private static SqlSessionFactory sqlSessionFactory;

@Rule
public ExpectedException expectedException = ExpectedException.none();

@BeforeClass
public static void setUp() throws Exception {
// create an SqlSessionFactory
Expand Down Expand Up @@ -73,4 +83,20 @@ public void shouldInsertListAndRetrieveId() throws Exception {
sqlSession.close();
}
}

@Test
public void shouldErrorUndefineProperty() {
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);

expectedException.expect(PersistenceException.class);
expectedException.expectMessage(containsString("### Error updating database. Cause: org.apache.ibatis.executor.ExecutorException: Error getting generated key or setting result to parameter object. Cause: org.apache.ibatis.executor.ExecutorException: No setter found for the keyProperty 'country_id' in org.apache.ibatis.submitted.keygen.Country."));

mapper.insertUndefineKeyProperty(new Country("China", "CN"));
} finally {
sqlSession.rollback();
sqlSession.close();
}
}
}

0 comments on commit 7164f15

Please sign in to comment.