diff --git a/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java b/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java index 72379fa6ac4..c089302a254 100644 --- a/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java +++ b/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java @@ -784,11 +784,10 @@ private boolean applyColumnOrderBasedConstructorAutomapping(ResultSetWrapper rsw List constructorArgs, Constructor constructor, boolean foundValues) throws SQLException { Class[] parameterTypes = constructor.getParameterTypes(); - // constructor parameter is allowed to be less than or equal to the number of result set columns, but not greater than if (parameterTypes.length > rsw.getClassNames().size()) { throw new ExecutorException(MessageFormat.format( - "Column order based constructor auto-mapping of ''{0}'' failed. Because result set type is ''{1}''.", - constructor, rsw.getClassNames())); + "Constructor auto-mapping of ''{0}'' failed. The constructor takes ''{1}'' arguments, but there are only ''{2}'' columns in the result set.", + constructor, parameterTypes.length, rsw.getClassNames().size())); } for (int i = 0; i < parameterTypes.length; i++) { diff --git a/src/test/java/org/apache/ibatis/submitted/column_order_based_constructor_automapping/ColumnOrderBasedConstructorAutomappingTest.java b/src/test/java/org/apache/ibatis/submitted/column_order_based_constructor_automapping/ColumnOrderBasedConstructorAutomappingTest.java index 30c914d97e4..f8fb4317965 100644 --- a/src/test/java/org/apache/ibatis/submitted/column_order_based_constructor_automapping/ColumnOrderBasedConstructorAutomappingTest.java +++ b/src/test/java/org/apache/ibatis/submitted/column_order_based_constructor_automapping/ColumnOrderBasedConstructorAutomappingTest.java @@ -20,8 +20,11 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; + import java.io.Reader; +import java.text.MessageFormat; import java.util.List; + import org.apache.ibatis.BaseDataTest; import org.apache.ibatis.exceptions.PersistenceException; import org.apache.ibatis.io.Resources; @@ -39,14 +42,14 @@ class ColumnOrderBasedConstructorAutomappingTest { static void setUp() throws Exception { // create an SqlSessionFactory try (Reader reader = Resources.getResourceAsReader( - "org/apache/ibatis/submitted/column_order_based_constructor_automapping/mybatis-config.xml")) { + "org/apache/ibatis/submitted/column_order_based_constructor_automapping/mybatis-config.xml")) { sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); sqlSessionFactory.getConfiguration().setArgNameBasedConstructorAutoMapping(false); } // populate in-memory database BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(), - "org/apache/ibatis/submitted/column_order_based_constructor_automapping/CreateDB.sql"); + "org/apache/ibatis/submitted/column_order_based_constructor_automapping/CreateDB.sql"); } @Test @@ -120,10 +123,14 @@ void shouldNotHandleConstructorGreaterThanResultSet() { try (SqlSession sqlSession = sqlSessionFactory.openSession()) { Mapper mapper = sqlSession.getMapper(Mapper.class); - PersistenceException persistenceException = assertThrows(PersistenceException.class, mapper::finaAllByConstructorGreaterThanResultSet); + PersistenceException persistenceException = assertThrows(PersistenceException.class, + mapper::finaAllByConstructorGreaterThanResultSet); assertNotNull(persistenceException); - assertNotNull(persistenceException.getMessage()); - assertTrue(persistenceException.getMessage().contains("Column order based constructor auto-mapping of")); + String message = persistenceException.getMessage(); + assertNotNull(message); + assertTrue(message.contains(MessageFormat.format( + "Constructor auto-mapping of ''{0}'' failed. The constructor takes ''{1}'' arguments, but there are only ''{2}'' columns in the result set.", + UserConstructorGreaterThanResultSet.class.getConstructors()[0], 4, 3))); } } }