Skip to content

Commit

Permalink
refs #364 Throws exception when collection type is ambiguous.
Browse files Browse the repository at this point in the history
  • Loading branch information
harawata committed Nov 6, 2018
1 parent 94272d7 commit 8e7b38a
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java
Expand Up @@ -405,13 +405,26 @@ private String processNestedResultMappings(XNode context, List<ResultMapping> re
|| "collection".equals(context.getName())
|| "case".equals(context.getName())) {
if (context.getStringAttribute("select") == null) {
validateCollection(context, enclosingType);
ResultMap resultMap = resultMapElement(context, resultMappings, enclosingType);
return resultMap.getId();
}
}
return null;
}

protected void validateCollection(XNode context, Class<?> enclosingType) {
if ("collection".equals(context.getName()) && context.getStringAttribute("resultMap") == null
&& context.getStringAttribute("resultType") == null) {

This comment has been minimized.

Copy link
@helloyou2012

helloyou2012 Jan 30, 2019

Contributor

<collection /> does not have resultType attribute. Is it javaType?

This comment has been minimized.

Copy link
@helloyou2012

helloyou2012 Jan 30, 2019

Contributor

PR #1472

MetaClass metaResultType = MetaClass.forClass(enclosingType, configuration.getReflectorFactory());
String property = context.getStringAttribute("property");
if (!metaResultType.hasSetter(property)) {
throw new BuilderException(
"Ambiguous collection type for property '" + property + "'. You must specify 'resultType' or 'resultMap'.");
}
}
}

private void bindMapperForNamespace() {
String namespace = builderAssistant.getCurrentNamespace();
if (namespace != null) {
Expand Down
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2018 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper
namespace="org.apache.ibatis.submitted.stringlist.MapperInvalid">

<resultMap type="map" id="invalidResultMap">
<id column="id" property="id" />
<!-- collection type is not resolvable -->
<collection property="groups" ofType="string">
<result column="group_id" />
</collection>
</resultMap>

</mapper>
Expand Up @@ -15,10 +15,13 @@
*/
package org.apache.ibatis.submitted.stringlist;

import static org.junit.Assert.*;

import java.io.Reader;
import java.util.List;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
Expand All @@ -40,7 +43,7 @@ public static void setUp() throws Exception {

// populate in-memory database
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/stringlist/CreateDB.sql");
"org/apache/ibatis/submitted/stringlist/CreateDB.sql");
}

@Test
Expand All @@ -54,4 +57,15 @@ public void shouldGetAUser() {
}
}

@Test
public void shouldFailFastIfCollectionTypeIsAmbiguous() throws Exception {
try (Reader reader = Resources
.getResourceAsReader("org/apache/ibatis/submitted/stringlist/mybatis-config-invalid.xml")) {
new SqlSessionFactoryBuilder().build(reader);
fail("Should throw exception when collection type is unresolvable.");
} catch (PersistenceException e) {
assertTrue(e.getMessage()
.contains("Ambiguous collection type for property 'groups'. You must specify 'resultType' or 'resultMap'."));
}
}
}
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2009-2018 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:stringlistinvalid" />
<property name="username" value="sa" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource="org/apache/ibatis/submitted/stringlist/MapperInvalid.xml" />
</mappers>

</configuration>

0 comments on commit 8e7b38a

Please sign in to comment.