Skip to content

gh-2834 result map regression #2841

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ private Class<?> resolveResultJavaType(Class<?> resultType, String property, Cla
if (javaType == null && property != null) {
try {
MetaClass metaResultType = MetaClass.forClass(resultType, configuration.getReflectorFactory());
javaType = metaResultType.getGetterType(property);
javaType = metaResultType.getSetterType(property);
} catch (Exception e) {
// ignore, following null check statement will deal with the situation
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/apache/ibatis/mapping/ResultMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public ResultMap build() {
if (actualArgNames == null) {
throw new BuilderException("Error in result map '" + resultMap.id + "'. Failed to find a constructor in '"
+ resultMap.getType().getName() + "' with arg names " + constructorArgNames
+ ". 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.");
+ ". 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.");
}
resultMap.constructorResultMappings.sort((o1, o2) -> {
int paramIdx1 = actualArgNames.indexOf(o1.getProperty());
Expand Down
2 changes: 1 addition & 1 deletion src/site/es/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ public class User {
</constructor>]]></source>

<p>
<code>javaType</code> can be omitted if there is a property with the same name and type.
<code>javaType</code> can be omitted if there is a writable property with the same name and type.
</p>

<p>El resto de atributos son los mismos que los de los elementos id y result.</p>
Expand Down
2 changes: 1 addition & 1 deletion src/site/ja/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,7 @@ public class User {
</constructor>]]></source>

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

<p>
Expand Down
2 changes: 1 addition & 1 deletion src/site/ko/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -986,7 +986,7 @@ public class User {
</constructor>]]></source>

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

<p>나머지 속성과 규칙은 id와 result엘리먼트와 동일하다.</p>
Expand Down
2 changes: 1 addition & 1 deletion src/site/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ public class User {
</constructor>]]></source>

<p>
<code>javaType</code> can be omitted if there is a property with the same name and type.
<code>javaType</code> can be omitted if there is a writable property with the same name and type.
</p>

<p>
Expand Down
2 changes: 1 addition & 1 deletion src/site/zh/xdoc/sqlmap-xml.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1135,7 +1135,7 @@ public class User {
</constructor>]]></source>

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

<p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@ public interface RecordTypeMapper {
@Select("select val, id, url from prop where id = #{id}")
Property selectProperty(int id);

@Arg(name = "id", column = "id", id = true)
@Arg(name = "value", column = "val")
@Arg(name = "URL", column = "url")
@Select("select val, id, url from prop where id = #{id}")
Property selectPropertyNoJavaType(int id);

@Insert("insert into prop (id, val, url) values (#{id}, #{value}, #{URL})")
int insertProperty(Property property);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,6 @@ void testSelectRecord() {
}
}

@Test
void shouldResolveConstructorArgType() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
RecordTypeMapper mapper = sqlSession.getMapper(RecordTypeMapper.class);
Property prop = mapper.selectPropertyNoJavaType(1);
assertEquals("Val1!", prop.value());
assertEquals("https://www.google.com", prop.URL());
}
}

@Test
void testSelectRecordAutomapping() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2009-2022 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
*
* https://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.
*/
package org.apache.ibatis.submitted.unmatched_prop_type;

import org.apache.ibatis.annotations.Arg;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;

public interface UnmatchedPropTypeMapper {

// javaType is required for 'id'
@Arg(id = true, column = "id", name = "id", javaType = String.class)
@Arg(column = "name", name = "name")
@Result(column = "dob", property = "dob")
@Select("select * from users where id = #{id}")
User getUser(Integer id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright 2009-2023 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
*
* https://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.
*/
package org.apache.ibatis.submitted.unmatched_prop_type;

import java.io.Reader;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class UnmatchedPropTypeTest {

private static SqlSessionFactory sqlSessionFactory;

@BeforeAll
static void setUp() throws Exception {
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/unmatched_prop_type/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/unmatched_prop_type/CreateDB.sql");
}

@Test
void shouldGetAUser() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
UnmatchedPropTypeMapper mapper = sqlSession.getMapper(UnmatchedPropTypeMapper.class);
User user = mapper.getUser(1);
Assertions.assertEquals("User1", user.getName());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2009-2022 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
*
* https://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.
*/
package org.apache.ibatis.submitted.unmatched_prop_type;

import java.time.LocalDate;

public class User {
private final Integer id;
private final String name;
private Birthday dob;

public User(String id, String name) {
super();
this.id = Integer.valueOf(id);
this.name = name;
}

public Integer getId() {
return id;
}

public String getName() {
return name;
}

public Birthday getDob() {
return dob;
}

public void setDob(String dob) {
this.dob = new Birthday(dob);
}

class Birthday {
private final LocalDate date;

Birthday(String date) {
this.date = LocalDate.parse(date);
}

public LocalDate getDate() {
return date;
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--
-- Copyright 2009-2022 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
--
-- https://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.
--

drop table users if exists;

create table users (
id int,
name varchar(20),
dob varchar(10)
);

insert into users (id, name, dob)
values (1, 'User1', '2000-01-01');
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--

Copyright 2009-2022 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

https://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"
"https://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:unmatched_prop_type" />
<property name="username" value="sa" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper class="org.apache.ibatis.submitted.unmatched_prop_type.UnmatchedPropTypeMapper" />
</mappers>

</configuration>