Skip to content

resultMap.hasResultMapsUsingConstructorCollection should stay false if type handler is specified #3391

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 3 commits into from
Jan 7, 2025
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
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/mapping/ResultMap.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2024 the original author or authors.
* Copyright 2009-2025 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.
Expand Down Expand Up @@ -116,7 +116,7 @@ public ResultMap build() {
// #101
Class<?> javaType = resultMapping.getJavaType();
resultMap.hasResultMapsUsingConstructorCollection = resultMap.hasResultMapsUsingConstructorCollection
|| (resultMapping.getNestedQueryId() == null && javaType != null
|| (resultMapping.getNestedQueryId() == null && resultMapping.getTypeHandler() == null && javaType != null
&& resultMap.configuration.getObjectFactory().isCollection(javaType));

if (resultMapping.getProperty() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package org.apache.ibatis.submitted.collection_in_constructor;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.tuple;

import java.io.Reader;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -164,6 +167,23 @@ void testCollectionArgWithTypeHandler() {
}
}

@Test
void testCollectionArgWithNestedAndTypeHandler() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
List<Store10> stores10 = mapper.getStores10();

assertThat(stores10).isNotNull().hasSize(3)
.extracting(Store10::getId, Store10::getName, store -> store.getClerks().size(), Store10::getStrings)
.containsExactly(tuple(1, "Store 1", 5, List.of("a", "b", "c", "1")),
tuple(2, "Store 2", 0, List.of("a", "b", "c", "2")), tuple(3, "Store 3", 0, List.of("a", "b", "c", "3")));

assertThat(stores10.get(0).getClerks()).extracting(Clerk::getId, Clerk::getName).containsExactly(
tuple(1001, "Clerk 1001"), tuple(1002, "Clerk 1002"), tuple(1003, "Clerk 1003"), tuple(1004, "Clerk 1004"),
tuple(1005, "Clerk 1005"));
}
}

@Test
void testImmutableNestedObjects() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Expand Down Expand Up @@ -203,17 +223,17 @@ void testImmutableNestedObjectsWithBadEquals() {
new Store9(1, "Store 1", Arrays.asList(new Clerk(1002, "Clerk 1002"), new Clerk(1005, "Clerk 1005")))));

// cannot use direct equals as we overwrote it with a bad impl on purpose
org.assertj.core.api.Assertions.assertThat(containers).isNotNull().hasSize(2);
assertThat(containers).isNotNull().hasSize(2);
assertContainer1(containers.get(0), expectedContainer1);
assertContainer1(containers.get(1), expectedContainer2);
}
}

private static void assertContainer1(Container1 container1, Container1 expectedContainer1) {
org.assertj.core.api.Assertions.assertThat(container1).isNotNull().satisfies(c -> {
org.assertj.core.api.Assertions.assertThat(c.getNum()).isEqualTo(expectedContainer1.getNum());
org.assertj.core.api.Assertions.assertThat(c.getType()).isEqualTo(expectedContainer1.getType());
org.assertj.core.api.Assertions.assertThat(c.getStores()).isEqualTo(expectedContainer1.getStores());
assertThat(container1).isNotNull().satisfies(c -> {
assertThat(c.getNum()).isEqualTo(expectedContainer1.getNum());
assertThat(c.getType()).isEqualTo(expectedContainer1.getType());
assertThat(c.getStores()).isEqualTo(expectedContainer1.getStores());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ public interface Mapper {

List<Container1> getContainers();

List<Store10> getStores10();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2009-2024 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.collection_in_constructor;

import java.util.List;
import java.util.Objects;

public class Store10 {

private final Integer id;
private final String name;
private final List<Clerk> clerks;
private final List<String> strings;

public Store10(Integer id, String name, List<Clerk> clerks, List<String> strings) {
this.id = id;
this.name = name;
this.clerks = clerks;
this.strings = strings;
}

public Integer getId() {
return id;
}

public String getName() {
return name;
}

public List<Clerk> getClerks() {
return clerks;
}

public List<String> getStrings() {
return strings;
}

@Override
public boolean equals(Object o) {
if (o == null || getClass() != o.getClass())
return false;
Store10 store10 = (Store10) o;
return Objects.equals(id, store10.id) && Objects.equals(name, store10.name)
&& Objects.equals(clerks, store10.clerks) && Objects.equals(strings, store10.strings);
}

@Override
public int hashCode() {
return Objects.hash(id, name, clerks, strings);
}

@Override
public String toString() {
return "Store10{" + "id=" + id + ", name='" + name + '\'' + ", clerks=" + clerks + ", strings=" + strings + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,4 +277,30 @@
order by type, s.id, c.id
]]></select>

<resultMap
type="org.apache.ibatis.submitted.collection_in_constructor.Store10"
id="store10RM">
<constructor>
<idArg column="id" javaType="int" />
<arg column="name" javaType="string" />
<arg javaType="list" resultMap="immutableClerkRM" columnPrefix="clerk_" />
<arg column="csv" javaType="list"
typeHandler="org.apache.ibatis.submitted.collection_in_constructor.CsvToListTypeHandler" />
</constructor>
</resultMap>

<select id="getStores10" resultMap="store10RM"
resultOrdered="true"><![CDATA[
select
s.id,
s.name,
c.id clerk_id, c.name clerk_name,
i.id aisle_id, i.name aisle_name,
CONCAT('a,b,c,', s.id) csv
from store s
left join clerk c on c.store_id = s.id
left join aisle i on i.store_id = s.id
order by s.id, c.id, i.id
]]></select>

</mapper>
Loading