Skip to content

Commit 472ef3d

Browse files
committed
Another corner case that causes BindingException.
1 parent a9c24fa commit 472ef3d

File tree

8 files changed

+231
-3
lines changed

8 files changed

+231
-3
lines changed

src/main/java/org/apache/ibatis/builder/annotation/MapperAnnotationBuilder.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.apache.ibatis.binding.BindingException;
6363
import org.apache.ibatis.binding.MapperMethod.ParamMap;
6464
import org.apache.ibatis.builder.BuilderException;
65+
import org.apache.ibatis.builder.CacheRefResolver;
6566
import org.apache.ibatis.builder.IncompleteElementException;
6667
import org.apache.ibatis.builder.MapperBuilderAssistant;
6768
import org.apache.ibatis.builder.xml.XMLMapperBuilder;
@@ -217,7 +218,11 @@ private void parseCacheRef() {
217218
throw new BuilderException("Cannot use both value() and name() attribute in the @CacheNamespaceRef");
218219
}
219220
String namespace = (refType != void.class) ? refType.getName() : refName;
220-
assistant.useCacheRef(namespace);
221+
try {
222+
assistant.useCacheRef(namespace);
223+
} catch (IncompleteElementException e) {
224+
configuration.addIncompleteCacheRef(new CacheRefResolver(assistant, namespace));
225+
}
221226
}
222227
}
223228

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,10 @@ protected void buildAllStatements() {
788788
}
789789
if (!incompleteMethods.isEmpty()) {
790790
synchronized (incompleteMethods) {
791-
// This always throws a BuilderException.
792-
incompleteMethods.iterator().next().resolve();
791+
incompleteMethods.removeIf(x -> {
792+
x.resolve();
793+
return true;
794+
});
793795
}
794796
}
795797
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Copyright 2009-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.resolution.javamethods;
17+
18+
import java.io.Reader;
19+
20+
import org.apache.ibatis.BaseDataTest;
21+
import org.apache.ibatis.io.Resources;
22+
import org.apache.ibatis.session.SqlSession;
23+
import org.apache.ibatis.session.SqlSessionFactory;
24+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
25+
import org.apache.ibatis.submitted.resolution.User;
26+
import org.junit.Assert;
27+
import org.junit.BeforeClass;
28+
import org.junit.Test;
29+
30+
public class JavaMethodsTest {
31+
32+
private static SqlSessionFactory sqlSessionFactory;
33+
34+
@BeforeClass
35+
public static void setUp() throws Exception {
36+
// create an SqlSessionFactory
37+
try (Reader reader = Resources
38+
.getResourceAsReader("org/apache/ibatis/submitted/resolution/javamethods/mybatis-config.xml")) {
39+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40+
}
41+
42+
// populate in-memory database
43+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
44+
"org/apache/ibatis/submitted/resolution/CreateDB.sql");
45+
}
46+
47+
@Test
48+
public void shouldGetAUser() {
49+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
50+
MapperB mapper = sqlSession.getMapper(MapperB.class);
51+
User user = mapper.getUser(1);
52+
Assert.assertEquals(Integer.valueOf(1), user.getId());
53+
Assert.assertEquals("User1", user.getName());
54+
}
55+
}
56+
57+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2009-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.resolution.javamethods;
17+
18+
import org.apache.ibatis.annotations.CacheNamespaceRef;
19+
import org.apache.ibatis.annotations.ResultMap;
20+
import org.apache.ibatis.annotations.Select;
21+
import org.apache.ibatis.submitted.resolution.User;
22+
23+
@CacheNamespaceRef(MapperC.class)
24+
public interface MapperA {
25+
@ResultMap("userRM")
26+
@Select("select * from users where id = #{id}")
27+
User getUser(Integer id);
28+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2018 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE mapper
20+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
23+
<mapper
24+
namespace="org.apache.ibatis.submitted.resolution.javamethods.MapperA">
25+
26+
<resultMap
27+
type="org.apache.ibatis.submitted.resolution.User" id="userRM">
28+
<id column="id" property="id" />
29+
<result column="name" property="name" />
30+
</resultMap>
31+
32+
</mapper>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2009-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.resolution.javamethods;
17+
18+
import org.apache.ibatis.annotations.CacheNamespaceRef;
19+
import org.apache.ibatis.annotations.ResultMap;
20+
import org.apache.ibatis.annotations.Select;
21+
import org.apache.ibatis.submitted.resolution.User;
22+
23+
@CacheNamespaceRef(MapperC.class)
24+
public interface MapperB {
25+
@ResultMap("org.apache.ibatis.submitted.resolution.javamethods.MapperA.userRM")
26+
@Select("select * from users where id = #{id}")
27+
User getUser(Integer id);
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Copyright 2009-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.resolution.javamethods;
17+
18+
import org.apache.ibatis.annotations.CacheNamespace;
19+
import org.apache.ibatis.annotations.ResultMap;
20+
import org.apache.ibatis.annotations.Select;
21+
import org.apache.ibatis.submitted.resolution.User;
22+
23+
@CacheNamespace
24+
public interface MapperC {
25+
@ResultMap("org.apache.ibatis.submitted.resolution.javamethods.MapperA.userRM")
26+
@Select("select * from users where id = #{id}")
27+
User getUser(Integer id);
28+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2018 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE configuration
20+
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-config.dtd">
22+
23+
<configuration>
24+
25+
<environments default="development">
26+
<environment id="development">
27+
<transactionManager type="JDBC">
28+
<property name="" value="" />
29+
</transactionManager>
30+
<dataSource type="UNPOOLED">
31+
<property name="driver" value="org.hsqldb.jdbcDriver" />
32+
<property name="url"
33+
value="jdbc:hsqldb:mem:javamethods" />
34+
<property name="username" value="sa" />
35+
</dataSource>
36+
</environment>
37+
</environments>
38+
39+
<mappers>
40+
<mapper
41+
class="org.apache.ibatis.submitted.resolution.javamethods.MapperA" />
42+
<mapper
43+
class="org.apache.ibatis.submitted.resolution.javamethods.MapperB" />
44+
<mapper
45+
class="org.apache.ibatis.submitted.resolution.javamethods.MapperC" />
46+
</mappers>
47+
48+
</configuration>

0 commit comments

Comments
 (0)