Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise a exception when keyProperty is not found #782

Merged
merged 1 commit into from
Sep 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}