Skip to content

Commit 352f58f

Browse files
committed
Wrong fix for http://code.google.com/p/mybatis/issues/detail?id=433 and a test that shows why it does not work
1 parent b117e02 commit 352f58f

File tree

7 files changed

+97
-20
lines changed

7 files changed

+97
-20
lines changed

src/main/java/org/apache/ibatis/executor/resultset/NestedResultSetHandler.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,6 @@ private boolean applyNestedResultMappings(ResultSet rs, ResultMap resultMap, Met
153153
final MetaObject targetMetaObject = configuration.newMetaObject(collectionProperty);
154154
targetMetaObject.add(rowValue);
155155
} else {
156-
if (!newObject) {
157-
throw new ExecutorException("Trying to overwrite a previous set value for the association '" + resultMapping.getProperty()
158-
+ "'. Check your id/result elements to ensure they identify uniquely an record.");
159-
}
160156
metaObject.setValue(resultMapping.getProperty(), rowValue);
161157
}
162158
foundValues = true;

src/test/java/org/apache/ibatis/submitted/associationtest/AssociationTest.java

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.sql.Connection;
2020
import java.util.List;
2121

22-
import org.apache.ibatis.exceptions.PersistenceException;
2322
import org.apache.ibatis.io.Resources;
2423
import org.apache.ibatis.jdbc.ScriptRunner;
2524
import org.apache.ibatis.session.SqlSession;
@@ -58,25 +57,43 @@ public void shouldGetAllCars() {
5857
Mapper mapper = sqlSession.getMapper(Mapper.class);
5958
List<Car> cars = mapper.getCars();
6059
Assert.assertEquals(4, cars.size());
60+
Assert.assertEquals("VW", cars.get(0).getType());
61+
Assert.assertNotNull(cars.get(0).getEngine());
62+
Assert.assertNull(cars.get(0).getBrakes());
63+
Assert.assertEquals("Opel", cars.get(1).getType());
64+
Assert.assertNull(cars.get(1).getEngine());
65+
Assert.assertNotNull(cars.get(1).getBrakes());
6166
} finally {
6267
sqlSession.close();
6368
}
6469
}
6570

66-
@Test(expected=PersistenceException.class)
71+
@Test
72+
public void shouldGetOneCarWithOneEngineAndBrakes() {
73+
SqlSession sqlSession = sqlSessionFactory.openSession();
74+
try {
75+
Mapper mapper = sqlSession.getMapper(Mapper.class);
76+
List<Car> cars = mapper.getCars2();
77+
Assert.assertEquals(1, cars.size());
78+
Assert.assertNotNull(cars.get(0).getEngine());
79+
Assert.assertNotNull(cars.get(0).getBrakes());
80+
} finally {
81+
sqlSession.close();
82+
}
83+
}
84+
85+
@Test
6786
public void shouldGetAllCarsNonUnique() {
6887
// this is a little weird - we might expect 4 objects back, but there are only
69-
// 2 unique combinations of Car attributes, so we get two back.
70-
71-
// update, this was reported as an error, see Issue #433
72-
// in this case there is data loss, so now it fails
88+
// 1 distinct carid, so we get one back.
7389
SqlSession sqlSession = sqlSessionFactory.openSession();
7490
try {
7591
Mapper mapper = sqlSession.getMapper(Mapper.class);
76-
List<Car> cars = mapper.getCarsNonUnique();
77-
Assert.assertEquals(2, cars.size());
92+
List<Car> cars = mapper.getCars2();
93+
Assert.assertEquals(1, cars.size());
7894
} finally {
7995
sqlSession.close();
8096
}
8197
}
98+
8299
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2009-2013 The MyBatis Team
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.associationtest;
17+
18+
public class Brakes {
19+
20+
private String type;
21+
22+
public String getType() {
23+
return type;
24+
}
25+
26+
public void setType(String type) {
27+
this.type = type;
28+
}
29+
30+
}

src/test/java/org/apache/ibatis/submitted/associationtest/Car.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
1+
/*
2+
* Copyright 2009-2013 The MyBatis Team
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+
*/
116
package org.apache.ibatis.submitted.associationtest;
217

318
public class Car {
419
private int id;
520
private String type;
621
private Engine engine;
22+
private Brakes brakes;
23+
724
public int getId() {
825
return id;
926
}
@@ -22,5 +39,11 @@ public Engine getEngine() {
2239
public void setEngine(Engine engine) {
2340
this.engine = engine;
2441
}
42+
public Brakes getBrakes() {
43+
return brakes;
44+
}
45+
public void setBrakes(Brakes brakes) {
46+
this.brakes = brakes;
47+
}
2548

2649
}

src/test/java/org/apache/ibatis/submitted/associationtest/CreateDB.sql

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ create table cars (
2020
carid integer,
2121
cartype varchar(20),
2222
enginetype varchar(20),
23-
enginecylinders integer
23+
enginecylinders integer,
24+
brakestype varchar(20)
2425
);
2526

26-
insert into cars (carid, cartype, enginetype, enginecylinders) values(1, 'VW', 'Diesel', 4);
27-
insert into cars (carid, cartype, enginetype, enginecylinders) values(2, 'VW', 'Gas', 6);
28-
insert into cars (carid, cartype, enginetype, enginecylinders) values(3, 'Audi', 'Diesel', 6);
29-
insert into cars (carid, cartype, enginetype, enginecylinders) values(4, 'Audi', 'Gas', 8);
27+
insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(1, 'VW', 'Diesel', 4, null);
28+
insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(2, 'Opel', null, null, 'drum');
29+
insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(3, 'Audi', 'Diesel', 4, 'disk');
30+
insert into cars (carid, cartype, enginetype, enginecylinders, brakestype) values(4, 'Ford', 'Gas', 8, 'drum');

src/test/java/org/apache/ibatis/submitted/associationtest/Mapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
public interface Mapper {
2121

2222
List<Car> getCars();
23-
List<Car> getCarsNonUnique();
23+
List<Car> getCars2();
24+
List<Car> getCars3();
2425

2526
}

src/test/java/org/apache/ibatis/submitted/associationtest/Mapper.xml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,29 @@
2121
<mapper namespace="org.apache.ibatis.submitted.associationtest.Mapper">
2222

2323
<resultMap type="org.apache.ibatis.submitted.associationtest.Car" id="carResult">
24-
<result column="carid" property="id"/>
24+
<id column="carid" property="id"/>
2525
<result column="cartype" property="type"/>
2626
<association property="engine" resultMap="engineResult"/>
27+
<association property="brakes" resultMap="brakesResult"/>
2728
</resultMap>
2829
<resultMap type="org.apache.ibatis.submitted.associationtest.Engine" id="engineResult">
2930
<result column="enginetype" property="type"/>
3031
<result column="enginecylinders" property="cylinders"/>
3132
</resultMap>
33+
<resultMap type="org.apache.ibatis.submitted.associationtest.Brakes" id="brakesResult">
34+
<result column="brakesType" property="type"/>
35+
</resultMap>
3236

3337
<select id="getCars" resultMap="carResult">
3438
select * from cars
3539
</select>
3640

3741
<select id="getCarsNonUnique" resultMap="carResult">
38-
select 1 as carid, cartype, enginetype, enginecylinders from cars
42+
select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars
43+
</select>
44+
45+
<select id="getCars2" resultMap="carResult">
46+
select 1 as carid, cartype, enginetype, enginecylinders, brakestype from cars where carid in (1,2)
3947
</select>
48+
4049
</mapper>

0 commit comments

Comments
 (0)