diff --git a/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java b/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java index 69889f5a602..49e8aa7c3f1 100644 --- a/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java +++ b/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java @@ -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); } } } diff --git a/src/test/java/org/apache/ibatis/submitted/keygen/CountryMapper.java b/src/test/java/org/apache/ibatis/submitted/keygen/CountryMapper.java index 8ca6b01069c..3943882d581 100644 --- a/src/test/java/org/apache/ibatis/submitted/keygen/CountryMapper.java +++ b/src/test/java/org/apache/ibatis/submitted/keygen/CountryMapper.java @@ -20,5 +20,6 @@ public interface CountryMapper { int insertList(List countries); + int insertUndefineKeyProperty(Country country); } diff --git a/src/test/java/org/apache/ibatis/submitted/keygen/CountryMapper.xml b/src/test/java/org/apache/ibatis/submitted/keygen/CountryMapper.xml index e98909f0857..1d08851f68f 100644 --- a/src/test/java/org/apache/ibatis/submitted/keygen/CountryMapper.xml +++ b/src/test/java/org/apache/ibatis/submitted/keygen/CountryMapper.xml @@ -27,4 +27,7 @@ (#{country.countryname},#{country.countrycode}) + + insert into country (countryname,countrycode) values (#{countryname},#{countrycode}) + diff --git a/src/test/java/org/apache/ibatis/submitted/keygen/Jdbc3KeyGeneratorTest.java b/src/test/java/org/apache/ibatis/submitted/keygen/Jdbc3KeyGeneratorTest.java index b1357608ee6..61bcc6e7b86 100644 --- a/src/test/java/org/apache/ibatis/submitted/keygen/Jdbc3KeyGeneratorTest.java +++ b/src/test/java/org/apache/ibatis/submitted/keygen/Jdbc3KeyGeneratorTest.java @@ -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 @@ -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 @@ -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(); + } + } }