Skip to content

Commit 2a422f3

Browse files
authored
Merge pull request #2841 from harawata/gh-2834-result-map-regression
gh-2834 result map regression
2 parents b83c0f2 + cfde857 commit 2a422f3

File tree

14 files changed

+216
-23
lines changed

14 files changed

+216
-23
lines changed

src/main/java/org/apache/ibatis/builder/MapperBuilderAssistant.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ private Class<?> resolveResultJavaType(Class<?> resultType, String property, Cla
437437
if (javaType == null && property != null) {
438438
try {
439439
MetaClass metaResultType = MetaClass.forClass(resultType, configuration.getReflectorFactory());
440-
javaType = metaResultType.getGetterType(property);
440+
javaType = metaResultType.getSetterType(property);
441441
} catch (Exception e) {
442442
// ignore, following null check statement will deal with the situation
443443
}

src/main/java/org/apache/ibatis/mapping/ResultMap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public ResultMap build() {
129129
if (actualArgNames == null) {
130130
throw new BuilderException("Error in result map '" + resultMap.id + "'. Failed to find a constructor in '"
131131
+ resultMap.getType().getName() + "' with arg names " + constructorArgNames
132-
+ ". Note that 'javaType' is required when there is no readable property with the same name ('name' is optional, BTW). There might be more info in debug log.");
132+
+ ". Note that 'javaType' is required when there is no writable property with the same name ('name' is optional, BTW). There might be more info in debug log.");
133133
}
134134
resultMap.constructorResultMappings.sort((o1, o2) -> {
135135
int paramIdx1 = actualArgNames.indexOf(o1.getProperty());

src/site/es/xdoc/sqlmap-xml.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ public class User {
986986
</constructor>]]></source>
987987

988988
<p>
989-
<code>javaType</code> can be omitted if there is a property with the same name and type.
989+
<code>javaType</code> can be omitted if there is a writable property with the same name and type.
990990
</p>
991991

992992
<p>El resto de atributos son los mismos que los de los elementos id y result.</p>

src/site/ja/xdoc/sqlmap-xml.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ public class User {
11021102
</constructor>]]></source>
11031103

11041104
<p>
1105-
引数と同じ名前、同じ型を持つプロパティが存在する場合 <code>javaType</code> は省略可能です。
1105+
引数と同じ名前、同じ型を持つ書き込み可能なプロパティが存在する場合 <code>javaType</code> は省略可能です。
11061106
</p>
11071107

11081108
<p>

src/site/ko/xdoc/sqlmap-xml.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,7 @@ public class User {
986986
</constructor>]]></source>
987987

988988
<p>
989-
같은 이름과 형태의 property 가 있는 경우는 <code>javaType</code> 를 생략 할 수 있다.
989+
같은 이름과 형태의 쓰기 가능한 property 가 있는 경우는 <code>javaType</code> 를 생략 할 수 있다.
990990
</p>
991991

992992
<p>나머지 속성과 규칙은 id와 result엘리먼트와 동일하다.</p>

src/site/xdoc/sqlmap-xml.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1190,7 +1190,7 @@ public class User {
11901190
</constructor>]]></source>
11911191

11921192
<p>
1193-
<code>javaType</code> can be omitted if there is a property with the same name and type.
1193+
<code>javaType</code> can be omitted if there is a writable property with the same name and type.
11941194
</p>
11951195

11961196
<p>

src/site/zh/xdoc/sqlmap-xml.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1135,7 +1135,7 @@ public class User {
11351135
</constructor>]]></source>
11361136

11371137
<p>
1138-
如果存在名称和类型相同的属性,那么可以省略 <code>javaType</code> 。
1138+
如果存在名称和类型相同的可写属性,那么可以省略 <code>javaType</code> 。
11391139
</p>
11401140

11411141
<p>

src/test/java/org/apache/ibatis/submitted/record_type/RecordTypeMapper.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ public interface RecordTypeMapper {
3232
@Select("select val, id, url from prop where id = #{id}")
3333
Property selectProperty(int id);
3434

35-
@Arg(name = "id", column = "id", id = true)
36-
@Arg(name = "value", column = "val")
37-
@Arg(name = "URL", column = "url")
38-
@Select("select val, id, url from prop where id = #{id}")
39-
Property selectPropertyNoJavaType(int id);
40-
4135
@Insert("insert into prop (id, val, url) values (#{id}, #{value}, #{URL})")
4236
int insertProperty(Property property);
4337

src/test/java/org/apache/ibatis/submitted/record_type/RecordTypeTest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@ void testSelectRecord() {
5252
}
5353
}
5454

55-
@Test
56-
void shouldResolveConstructorArgType() {
57-
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
58-
RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
59-
Property prop = mapper.selectPropertyNoJavaType(1);
60-
assertEquals("Val1!", prop.value());
61-
assertEquals("https://www.google.com", prop.URL());
62-
}
63-
}
64-
6555
@Test
6656
void testSelectRecordAutomapping() {
6757
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2009-2022 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+
* https://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.unmatched_prop_type;
17+
18+
import org.apache.ibatis.annotations.Arg;
19+
import org.apache.ibatis.annotations.Result;
20+
import org.apache.ibatis.annotations.Select;
21+
22+
public interface UnmatchedPropTypeMapper {
23+
24+
// javaType is required for 'id'
25+
@Arg(id = true, column = "id", name = "id", javaType = String.class)
26+
@Arg(column = "name", name = "name")
27+
@Result(column = "dob", property = "dob")
28+
@Select("select * from users where id = #{id}")
29+
User getUser(Integer id);
30+
31+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2009-2023 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+
* https://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.unmatched_prop_type;
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.junit.jupiter.api.Assertions;
26+
import org.junit.jupiter.api.BeforeAll;
27+
import org.junit.jupiter.api.Test;
28+
29+
class UnmatchedPropTypeTest {
30+
31+
private static SqlSessionFactory sqlSessionFactory;
32+
33+
@BeforeAll
34+
static void setUp() throws Exception {
35+
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/unmatched_prop_type/mybatis-config.xml")) {
36+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
37+
}
38+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
39+
"org/apache/ibatis/submitted/unmatched_prop_type/CreateDB.sql");
40+
}
41+
42+
@Test
43+
void shouldGetAUser() {
44+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
45+
UnmatchedPropTypeMapper mapper = sqlSession.getMapper(UnmatchedPropTypeMapper.class);
46+
User user = mapper.getUser(1);
47+
Assertions.assertEquals("User1", user.getName());
48+
}
49+
}
50+
51+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2009-2022 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+
* https://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.unmatched_prop_type;
17+
18+
import java.time.LocalDate;
19+
20+
public class User {
21+
private final Integer id;
22+
private final String name;
23+
private Birthday dob;
24+
25+
public User(String id, String name) {
26+
super();
27+
this.id = Integer.valueOf(id);
28+
this.name = name;
29+
}
30+
31+
public Integer getId() {
32+
return id;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public Birthday getDob() {
40+
return dob;
41+
}
42+
43+
public void setDob(String dob) {
44+
this.dob = new Birthday(dob);
45+
}
46+
47+
class Birthday {
48+
private final LocalDate date;
49+
50+
Birthday(String date) {
51+
this.date = LocalDate.parse(date);
52+
}
53+
54+
public LocalDate getDate() {
55+
return date;
56+
}
57+
58+
}
59+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--
2+
-- Copyright 2009-2022 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+
-- https://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 users if exists;
18+
19+
create table users (
20+
id int,
21+
name varchar(20),
22+
dob varchar(10)
23+
);
24+
25+
insert into users (id, name, dob)
26+
values (1, 'User1', '2000-01-01');
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-2022 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+
https://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+
"https://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:unmatched_prop_type" />
33+
<property name="username" value="sa" />
34+
</dataSource>
35+
</environment>
36+
</environments>
37+
38+
<mappers>
39+
<mapper class="org.apache.ibatis.submitted.unmatched_prop_type.UnmatchedPropTypeMapper" />
40+
</mappers>
41+
42+
</configuration>

0 commit comments

Comments
 (0)