Skip to content

Commit

Permalink
Merge pull request #2913 from harawata/constructor-resultmap-discrimi…
Browse files Browse the repository at this point in the history
…nator

Support result map with discriminator in constructor mapping
  • Loading branch information
harawata authored Jul 17, 2023
2 parents 286b302 + 5ffd40c commit 5f61917
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -704,8 +704,10 @@ Object createParameterizedResultObject(ResultSetWrapper rsw, Class<?> resultType
if (constructorMapping.getNestedQueryId() != null) {
value = getNestedQueryConstructorValue(rsw.getResultSet(), constructorMapping, columnPrefix);
} else if (constructorMapping.getNestedResultMapId() != null) {
final ResultMap resultMap = configuration.getResultMap(constructorMapping.getNestedResultMapId());
value = getRowValue(rsw, resultMap, getColumnPrefix(columnPrefix, constructorMapping));
String constructorColumnPrefix = getColumnPrefix(columnPrefix, constructorMapping);
final ResultMap resultMap = resolveDiscriminatedResultMap(rsw.getResultSet(),
configuration.getResultMap(constructorMapping.getNestedResultMapId()), constructorColumnPrefix);
value = getRowValue(rsw, resultMap, constructorColumnPrefix);
} else {
final TypeHandler<?> typeHandler = constructorMapping.getTypeHandler();
value = typeHandler.getResult(rsw.getResultSet(), prependPrefix(column, columnPrefix));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.discriminator;

public class Contract {
private Integer id;
private Owner owner;

public Contract(Integer id, Owner owner) {
super();
this.id = id;
this.owner = owner;
}

public Integer getId() {
return id;
}

public Owner getOwner() {
return owner;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,25 @@ void shouldInheritResultType() {
}
}

@Test
void shouldBeAppliedToResultMapInConstructorArg() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
List<Owner> owners = mapper.selectOwnersWithAVehicleConstructor();
assertEquals(Truck.class, owners.get(0).getVehicle().getClass());
assertEquals(Car.class, owners.get(1).getVehicle().getClass());
}
}

@Test
void shouldBeAppliedToResultMapInConstructorArgNested() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Mapper mapper = sqlSession.getMapper(Mapper.class);
List<Contract> contracts = mapper.selectContracts();
assertEquals(2, contracts.size());
assertEquals(Truck.class, contracts.get(0).getOwner().getVehicle().getClass());
assertEquals(Car.class, contracts.get(1).getOwner().getVehicle().getClass());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ public interface Mapper {

List<Owner> selectOwnersWithAVehicle();

List<Owner> selectOwnersWithAVehicleConstructor();

List<Contract> selectContracts();

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2022 the original author or authors.
* 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.
Expand All @@ -20,6 +20,16 @@ public class Owner {
private String name;
private Vehicle vehicle;

public Owner() {
super();
}

public Owner(Integer id, Vehicle vehicle) {
super();
this.id = id;
this.vehicle = vehicle;
}

public Integer getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--
-- Copyright 2009-2022 the original author or authors.
-- 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.
Expand All @@ -16,6 +16,7 @@

drop table vehicle if exists;
drop table owner if exists;
drop table contract if exists;

create table vehicle (
id int,
Expand All @@ -39,3 +40,12 @@ create table owner (
insert into owner (id, name, vehicle_type, vehicle_id) values
(1, 'Owner1', 'truck', 2),
(2, 'Owner2', 'car', 1);

create table contract (
id int,
owner_id int
);

insert into contract (id, owner_id) values
(11, 1),
(12, 2);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2009-2022 the original author or authors.
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.
Expand Down Expand Up @@ -81,4 +81,50 @@
where id = #{id}
]]></select>

<resultMap
type="org.apache.ibatis.submitted.discriminator.Owner"
id="ownerConstructorResult">
<constructor>
<idArg javaType="int" column="id" />
<arg javaType="org.apache.ibatis.submitted.discriminator.Vehicle"
resultMap="vehicleResult" columnPrefix="vhc_" />
</constructor>
<result javaType="string" column="name" />
</resultMap>

<select id="selectOwnersWithAVehicleConstructor"
resultMap="ownerConstructorResult"><![CDATA[
select onr.id, onr.name,
vhc.id vhc_id,
vhc.maker vhc_maker,
vhc.vehicle_type vhc_vehicle_type,
vhc.door_count vhc_door_count,
vhc.carrying_capacity vhc_carrying_capacity
from owner onr
left join vehicle vhc on vhc.id = onr.vehicle_id
]]></select>

<resultMap
type="org.apache.ibatis.submitted.discriminator.Contract"
id="contractResult">
<constructor>
<idArg javaType="int" column="id" />
<arg javaType="org.apache.ibatis.submitted.discriminator.Owner"
resultMap="ownerConstructorResult" columnPrefix="onr_" />
</constructor>
</resultMap>

<select id="selectContracts" resultMap="contractResult"><![CDATA[
select ctt.id,
onr.id onr_id, onr.name onr_name,
vhc.id onr_vhc_id,
vhc.maker onr_vhc_maker,
vhc.vehicle_type onr_vhc_vehicle_type,
vhc.door_count onr_vhc_door_count,
vhc.carrying_capacity onr_vhc_carrying_capacity
from contract ctt
left join owner onr on onr.id = ctt.owner_id
left join vehicle vhc on vhc.id = onr.vehicle_id
]]></select>

</mapper>

0 comments on commit 5f61917

Please sign in to comment.