Skip to content
Closed
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
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,7 +61,7 @@ public void processAfter(Executor executor, MappedStatement ms, Statement stmt,
public void processBatch(MappedStatement ms, Statement stmt, Object parameter) {
final String[] keyProperties = ms.getKeyProperties();
if (keyProperties == null || keyProperties.length == 0) {
return;
throw new ExecutorException("Could not assign generated keys. Please specify 'keyProperty'.");
}
try (ResultSet rs = stmt.getGeneratedKeys()) {
final Configuration configuration = ms.getConfiguration();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -33,6 +33,10 @@ public interface CountryMapper {
@Insert({ "insert into country (countryname,countrycode) values (#{country.countryname},#{country.countrycode})" })
int insertNamedBean(@Param("country") Country country);

@Options(useGeneratedKeys = true)
@Insert({ "insert into country (countryname,countrycode) values (#{countryname},#{countrycode})" })
int insertBean_MissingKeyProperty(Country country);

int insertList(List<Country> countries);

int insertNamedList(@Param("countries") List<Country> countries);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2018 the original author or authors.
* Copyright 2009-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -208,6 +208,21 @@ public void shouldAssignKeyToBean_MultiParams() throws Exception {
}
}

@Test
public void shouldFailIfKeyPropertyIsMissing() throws Exception {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
try {
CountryMapper mapper = sqlSession.getMapper(CountryMapper.class);
Country country = new Country("China", "CN");
when(mapper).insertBean_MissingKeyProperty(country);
then(caughtException()).isInstanceOf(PersistenceException.class).hasMessageContaining(
"Could not assign generated keys. Please specify 'keyProperty'.");
} finally {
sqlSession.rollback();
}
}
}

@Test
public void shouldFailIfKeyPropertyIsInvalid_NoParamName() throws Exception {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Expand Down