Skip to content

Commit 76826c8

Browse files
committed
fixes #1495 Constructor auto-mapping does not need columnPrefix.
1 parent 8e2df26 commit 76826c8

File tree

8 files changed

+304
-7
lines changed

8 files changed

+304
-7
lines changed

src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ private Object createResultObject(ResultSetWrapper rsw, ResultMap resultMap, Lis
615615
} else if (resultType.isInterface() || metaType.hasDefaultConstructor()) {
616616
return objectFactory.create(resultType);
617617
} else if (shouldApplyAutomaticMappings(resultMap, false)) {
618-
return createByConstructorSignature(rsw, resultType, constructorArgTypes, constructorArgs, columnPrefix);
618+
return createByConstructorSignature(rsw, resultType, constructorArgTypes, constructorArgs);
619619
}
620620
throw new ExecutorException("Do not know how to create an instance of " + resultType);
621621
}
@@ -647,29 +647,28 @@ Object createParameterizedResultObject(ResultSetWrapper rsw, Class<?> resultType
647647
return foundValues ? objectFactory.create(resultType, constructorArgTypes, constructorArgs) : null;
648648
}
649649

650-
private Object createByConstructorSignature(ResultSetWrapper rsw, Class<?> resultType, List<Class<?>> constructorArgTypes, List<Object> constructorArgs,
651-
String columnPrefix) throws SQLException {
650+
private Object createByConstructorSignature(ResultSetWrapper rsw, Class<?> resultType, List<Class<?>> constructorArgTypes, List<Object> constructorArgs) throws SQLException {
652651
final Constructor<?>[] constructors = resultType.getDeclaredConstructors();
653652
final Constructor<?> defaultConstructor = findDefaultConstructor(constructors);
654653
if (defaultConstructor != null) {
655-
return createUsingConstructor(rsw, resultType, constructorArgTypes, constructorArgs, columnPrefix, defaultConstructor);
654+
return createUsingConstructor(rsw, resultType, constructorArgTypes, constructorArgs, defaultConstructor);
656655
} else {
657656
for (Constructor<?> constructor : constructors) {
658657
if (allowedConstructorUsingTypeHandlers(constructor, rsw.getJdbcTypes())) {
659-
return createUsingConstructor(rsw, resultType, constructorArgTypes, constructorArgs, columnPrefix, constructor);
658+
return createUsingConstructor(rsw, resultType, constructorArgTypes, constructorArgs, constructor);
660659
}
661660
}
662661
}
663662
throw new ExecutorException("No constructor found in " + resultType.getName() + " matching " + rsw.getClassNames());
664663
}
665664

666-
private Object createUsingConstructor(ResultSetWrapper rsw, Class<?> resultType, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, String columnPrefix, Constructor<?> constructor) throws SQLException {
665+
private Object createUsingConstructor(ResultSetWrapper rsw, Class<?> resultType, List<Class<?>> constructorArgTypes, List<Object> constructorArgs, Constructor<?> constructor) throws SQLException {
667666
boolean foundValues = false;
668667
for (int i = 0; i < constructor.getParameterTypes().length; i++) {
669668
Class<?> parameterType = constructor.getParameterTypes()[i];
670669
String columnName = rsw.getColumnNames().get(i);
671670
TypeHandler<?> typeHandler = rsw.getTypeHandler(parameterType, columnName);
672-
Object value = typeHandler.getResult(rsw.getResultSet(), prependPrefix(columnName, columnPrefix));
671+
Object value = typeHandler.getResult(rsw.getResultSet(), columnName);
673672
constructorArgTypes.add(parameterType);
674673
constructorArgs.add(value);
675674
foundValues = value != null || foundValues;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2009-2019 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.constructor_automapping;
17+
18+
public class Article {
19+
20+
private Integer id;
21+
private String title;
22+
private Author author;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getTitle() {
33+
return title;
34+
}
35+
36+
public void setTitle(String title) {
37+
this.title = title;
38+
}
39+
40+
public Author getAuthor() {
41+
return author;
42+
}
43+
44+
public void setAuthor(Author author) {
45+
this.author = author;
46+
}
47+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright 2009-2019 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.constructor_automapping;
17+
18+
public class Author {
19+
20+
private final Integer id;
21+
private String name;
22+
23+
private Author(Integer id) {
24+
super();
25+
this.id = id;
26+
}
27+
28+
public Integer getId() {
29+
return id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/**
2+
* Copyright 2009-2019 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.constructor_automapping;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
import java.io.Reader;
21+
import java.util.List;
22+
23+
import org.apache.ibatis.BaseDataTest;
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.jupiter.api.BeforeAll;
29+
import org.junit.jupiter.api.Test;
30+
31+
class ConstructorAutomappingTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeAll
36+
static void setUp() throws Exception {
37+
// create an SqlSessionFactory
38+
try (Reader reader = Resources
39+
.getResourceAsReader("org/apache/ibatis/submitted/constructor_automapping/mybatis-config.xml")) {
40+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
41+
}
42+
43+
// populate in-memory database
44+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
45+
"org/apache/ibatis/submitted/constructor_automapping/CreateDB.sql");
46+
}
47+
48+
@Test
49+
void shouldHandleColumnPrefixCorrectly() {
50+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
51+
Mapper mapper = sqlSession.getMapper(Mapper.class);
52+
List<Article> articles = mapper.nestedConstructorAutomapping();
53+
assertEquals(2, articles.size());
54+
Article article1 = articles.get(0);
55+
assertEquals("Article1", article1.getTitle());
56+
Author author1 = article1.getAuthor();
57+
assertEquals(Integer.valueOf(100), author1.getId());
58+
assertEquals("Author1", author1.getName());
59+
}
60+
}
61+
62+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--
2+
-- Copyright 2009-2019 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+
17+
drop table articles if exists;
18+
drop table authors if exists;
19+
20+
create table articles (
21+
id int,
22+
title varchar(20),
23+
author_id int
24+
);
25+
26+
create table authors (
27+
id int,
28+
name varchar(20)
29+
);
30+
31+
insert into articles (id, title, author_id) values
32+
(1, 'Article1', 100),
33+
(2, 'Article2', 200);
34+
35+
insert into authors (id, name) values
36+
(100, 'Author1'),
37+
(200, 'Author2');
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright 2009-2019 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.constructor_automapping;
17+
18+
import java.util.List;
19+
20+
public interface Mapper {
21+
22+
List<Article> nestedConstructorAutomapping();
23+
24+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2019 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.constructor_automapping.Mapper">
25+
26+
<resultMap
27+
type="org.apache.ibatis.submitted.constructor_automapping.Article"
28+
id="articleRM">
29+
<result property="id" column="id" />
30+
<result property="title" column="title" />
31+
<association property="author" resultMap="authorRM"
32+
columnPrefix="author_" />
33+
</resultMap>
34+
35+
<resultMap autoMapping="true"
36+
type="org.apache.ibatis.submitted.constructor_automapping.Author"
37+
id="authorRM">
38+
</resultMap>
39+
40+
<select id="nestedConstructorAutomapping" resultMap="articleRM"><![CDATA[
41+
select authors.id aid, authors.name author_name,
42+
articles.id, articles.title
43+
from articles
44+
left join authors on authors.id = author_id
45+
]]></select>
46+
47+
</mapper>
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<!--
3+
4+
Copyright 2009-2019 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" value="jdbc:hsqldb:mem:constructorautomapping" />
33+
<property name="username" value="sa" />
34+
</dataSource>
35+
</environment>
36+
</environments>
37+
38+
<mappers>
39+
<mapper class="org.apache.ibatis.submitted.constructor_automapping.Mapper" />
40+
</mappers>
41+
42+
</configuration>

0 commit comments

Comments
 (0)