Skip to content

Commit 94272d7

Browse files
committed
fixes #486 When there is no 'resultType' nor 'resultMap' specified for <association />, use the result map's type.
1 parent c797058 commit 94272d7

File tree

10 files changed

+407
-0
lines changed

10 files changed

+407
-0
lines changed

src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ protected Class<?> inheritEnclosingType(XNode resultMapNode, Class<?> enclosingT
300300
MetaClass metaResultType = MetaClass.forClass(enclosingType, configuration.getReflectorFactory());
301301
return metaResultType.getSetterType(property);
302302
}
303+
} else if ("case".equals(resultMapNode.getName()) && resultMapNode.getStringAttribute("resultMap") == null) {
304+
return enclosingType;
303305
}
304306
return null;
305307
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2009-2018 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+
* http://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+
package org.apache.ibatis.submitted.discriminator;
18+
19+
public class Car extends Vehicle {
20+
protected Integer doorCount;
21+
22+
public Integer getDoorCount() {
23+
return doorCount;
24+
}
25+
26+
public void setDoorCount(Integer doorCount) {
27+
this.doorCount = doorCount;
28+
}
29+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--
2+
-- Copyright 2009-2018 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+
-- http://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 vehicle if exists;
18+
drop table owner if exists;
19+
20+
create table vehicle (
21+
id int,
22+
maker varchar(20),
23+
vehicle_type int,
24+
door_count int,
25+
carrying_capacity float
26+
);
27+
28+
insert into vehicle (id, maker, vehicle_type, door_count, carrying_capacity) values
29+
(1, 'Maker1', 1, 5, null),
30+
(2, 'Maker2', 2, null, 1.5);
31+
32+
create table owner (
33+
id int,
34+
name varchar(20),
35+
vehicle_type varchar(20),
36+
vehicle_id int
37+
);
38+
39+
insert into owner (id, name, vehicle_type, vehicle_id) values
40+
(1, 'Owner1', 'truck', 2),
41+
(2, 'Owner2', 'car', 1);
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2009-2018 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+
* http://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.discriminator;
17+
18+
import static org.junit.Assert.*;
19+
20+
import java.io.Reader;
21+
import java.util.List;
22+
23+
import org.apache.ibatis.BaseDataTest;
24+
import org.apache.ibatis.io.Resources;
25+
import org.apache.ibatis.session.SqlSession;
26+
import org.apache.ibatis.session.SqlSessionFactory;
27+
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
28+
import org.junit.BeforeClass;
29+
import org.junit.Test;
30+
31+
public class DiscriminatorTest {
32+
33+
private static SqlSessionFactory sqlSessionFactory;
34+
35+
@BeforeClass
36+
public static void setUp() throws Exception {
37+
// create an SqlSessionFactory
38+
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/discriminator/mybatis-config.xml")) {
39+
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
40+
}
41+
42+
// populate in-memory database
43+
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
44+
"org/apache/ibatis/submitted/discriminator/CreateDB.sql");
45+
}
46+
47+
@Test
48+
public void shouldSwitchResultType() {
49+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
50+
Mapper mapper = sqlSession.getMapper(Mapper.class);
51+
List<Vehicle> vehicles = mapper.selectVehicles();
52+
assertEquals(Car.class, vehicles.get(0).getClass());
53+
assertEquals(Integer.valueOf(5), ((Car)vehicles.get(0)).getDoorCount());
54+
assertEquals(Truck.class, vehicles.get(1).getClass());
55+
assertEquals(Float.valueOf(1.5f), ((Truck)vehicles.get(1)).getCarryingCapacity());
56+
}
57+
}
58+
59+
@Test
60+
public void shouldInheritResultType() {
61+
// #486
62+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
63+
Mapper mapper = sqlSession.getMapper(Mapper.class);
64+
List<Owner> owners = mapper.selectOwnersWithAVehicle();
65+
assertEquals(Truck.class, owners.get(0).getVehicle().getClass());
66+
assertEquals(Car.class, owners.get(1).getVehicle().getClass());
67+
}
68+
}
69+
70+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Copyright 2009-2018 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+
* http://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.discriminator;
17+
18+
import java.util.List;
19+
20+
public interface Mapper {
21+
22+
List<Vehicle> selectVehicles();
23+
List<Owner> selectOwnersWithAVehicle();
24+
25+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2018 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+
http://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 mapper
20+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
23+
<mapper
24+
namespace="org.apache.ibatis.submitted.discriminator.Mapper">
25+
26+
<resultMap
27+
type="org.apache.ibatis.submitted.discriminator.Vehicle"
28+
id="vehicleResult">
29+
<id property="id" column="id" />
30+
<result property="maker" column="maker" />
31+
<discriminator javaType="int" column="vehicle_type">
32+
<case value="1"
33+
resultType="org.apache.ibatis.submitted.discriminator.Car">
34+
<result property="doorCount" column="door_count" />
35+
</case>
36+
<case value="2"
37+
resultType="org.apache.ibatis.submitted.discriminator.Truck">
38+
<result property="carryingCapacity"
39+
column="carrying_capacity" />
40+
</case>
41+
</discriminator>
42+
</resultMap>
43+
44+
<select id="selectVehicles" resultMap="vehicleResult"><![CDATA[
45+
select * from vehicle order by id
46+
]]></select>
47+
48+
<resultMap
49+
type="org.apache.ibatis.submitted.discriminator.Owner"
50+
id="ownerResult">
51+
<id property="id" column="id" />
52+
<result property="name" column="name" />
53+
<discriminator javaType="string" column="vehicle_type">
54+
<case value="car">
55+
<association property="vehicle" column="vehicle_id"
56+
select="selectCar" />
57+
</case>
58+
<case value="truck">
59+
<association property="vehicle" column="vehicle_id"
60+
select="selectTruck" />
61+
</case>
62+
</discriminator>
63+
</resultMap>
64+
65+
<select id="selectOwnersWithAVehicle" resultMap="ownerResult"><![CDATA[
66+
select id, name, vehicle_type, vehicle_id
67+
from owner
68+
]]></select>
69+
70+
<select id="selectCar"
71+
resultType="org.apache.ibatis.submitted.discriminator.Car"><![CDATA[
72+
select id, maker, door_count doorCount
73+
from vehicle
74+
where id = #{id}
75+
]]></select>
76+
77+
<select id="selectTruck"
78+
resultType="org.apache.ibatis.submitted.discriminator.Truck"><![CDATA[
79+
select id, maker, door_count carryingCapacity
80+
from vehicle
81+
where id = #{id}
82+
]]></select>
83+
84+
</mapper>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Copyright 2009-2018 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+
* http://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+
package org.apache.ibatis.submitted.discriminator;
18+
19+
public class Owner {
20+
private Integer id;
21+
private String name;
22+
private Vehicle vehicle;
23+
24+
public Integer getId() {
25+
return id;
26+
}
27+
28+
public void setId(Integer id) {
29+
this.id = id;
30+
}
31+
32+
public String getName() {
33+
return name;
34+
}
35+
36+
public void setName(String name) {
37+
this.name = name;
38+
}
39+
40+
public Vehicle getVehicle() {
41+
return vehicle;
42+
}
43+
44+
public void setVehicle(Vehicle vehicle) {
45+
this.vehicle = vehicle;
46+
}
47+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Copyright 2009-2018 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+
* http://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+
package org.apache.ibatis.submitted.discriminator;
18+
19+
public class Truck extends Vehicle {
20+
protected Float carryingCapacity;
21+
22+
public Float getCarryingCapacity() {
23+
return carryingCapacity;
24+
}
25+
26+
public void setCarryingCapacity(Float carryingCapacity) {
27+
this.carryingCapacity = carryingCapacity;
28+
}
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Copyright 2009-2018 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+
* http://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+
package org.apache.ibatis.submitted.discriminator;
18+
19+
public class Vehicle {
20+
protected Integer id;
21+
protected String maker;
22+
23+
public Integer getId() {
24+
return id;
25+
}
26+
27+
public void setId(Integer id) {
28+
this.id = id;
29+
}
30+
31+
public String getMaker() {
32+
return maker;
33+
}
34+
35+
public void setMaker(String maker) {
36+
this.maker = maker;
37+
}
38+
}

0 commit comments

Comments
 (0)