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 13e5d45a7bd..fe47de014cf 100644 --- a/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java +++ b/src/main/java/org/apache/ibatis/executor/keygen/Jdbc3KeyGenerator.java @@ -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. @@ -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(); 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 8f1e90557fe..ccb0053171b 100644 --- a/src/test/java/org/apache/ibatis/submitted/keygen/CountryMapper.java +++ b/src/test/java/org/apache/ibatis/submitted/keygen/CountryMapper.java @@ -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. @@ -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 countries); int insertNamedList(@Param("countries") List countries); 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 6d6d61531b4..41c673a465d 100644 --- a/src/test/java/org/apache/ibatis/submitted/keygen/Jdbc3KeyGeneratorTest.java +++ b/src/test/java/org/apache/ibatis/submitted/keygen/Jdbc3KeyGeneratorTest.java @@ -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. @@ -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()) {