Skip to content

Commit

Permalink
Merge branch 'master' into feat/2618-auto-determine-type-when-missing
Browse files Browse the repository at this point in the history
  • Loading branch information
epochcoder committed Jan 12, 2025
2 parents 009e7dd + b13e7d8 commit 98665bc
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 8 deletions.
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 @@ -115,7 +115,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));
} else {
resultMap.propertyResultMappings.add(resultMapping);
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 @@ -19,6 +19,7 @@

import org.apache.ibatis.datasource.unpooled.UnpooledDataSource;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

public final class PgContainer {

Expand All @@ -31,8 +32,9 @@ public final class PgContainer {

private static PostgreSQLContainer<?> initContainer() {
@SuppressWarnings("resource")
PostgreSQLContainer<?> container = new PostgreSQLContainer<>("postgres").withDatabaseName(DB_NAME)
.withUsername(USERNAME).withPassword(PASSWORD);
PostgreSQLContainer<?> container = new PostgreSQLContainer<>(
DockerImageName.parse("postgres").withTag(PostgreSQLContainer.DEFAULT_TAG)).withDatabaseName(DB_NAME)
.withUsername(USERNAME).withPassword(PASSWORD);
container.start();
return container;
}
Expand Down
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>

0 comments on commit 98665bc

Please sign in to comment.