Details
Description
what I am seeing is the nested select is not getting executed. To make it simpler, I created a simple prototype. I know in this example I don't need to create a separate select, but wondering why its not working. Here are the details
POJOs
public class Car
{
private int carId ;
private String name ;
private List<Parts> carParts ;
}
public class Parts
{
private int partId;
private String name ;
}
==============
Mapper
<resultMap id="carResult" type="Car">
<id property="carId" column="id"/>
<result property="name" column="name"/>
<collection property="carParts" javaType="ArrayList" ofType="Parts" select="getCarPartInfo"/>
</resultMap>
<select id="getCarsInfo" resultMap="carResult">
SELECT car_id as "id", name
FROM Car where car_id=1
</select>
<select id="getCarPartInfo" resultType="Parts">
SELECT part_id as "partId", name
FROM Parts where car_id=1
</select>
==========
Tables
======
mysql> select * from car;
------------+
car_id | name |
------------+
1 | Audi |
------------+
1 row in set (0.00 sec)
mysql> select * from Parts;
-------------------------
part_id | name | car_id |
-------------------------
100 | door | 1 |
101 | windshield | 1 |
102 | Brakes | 1 |
-------------------------
3 rows in set (0.00 sec)
=========================
Unit Test
public void testGetCarInfo() {
List<Car> list = repository.getCarsInfo();
for (Iterator<Car> carIter = list.iterator();carIter.hasNext() {
Car d = carIter.next();
logger.debug("Car id " + d.getCarId() + " name " + d.getName());
List<Parts> dPartList = d.getCarParts();
if (dPartList!=null)
else
{ logger.debug(" car Part List is null " ); }}
assertNotNull("At least one device must exist", list);
}
Unit Test Result
2010-04-07 11:27:11,576 DEBUG [iBatisImplTest] Car id 1 name Audi
2010-04-07 11:27:11,577 DEBUG [iBatisImplTest] car Part List is null
As you can see the result does not contain the Parts List, the Collection is not getting set. It does not throw any warning, error or exception simply returns empty list.
The reason was because column attribute was missing from Collection. when I added <collection property="carParts" column="id" javaType="ArrayList" ofType="Parts" select="getCarPartInfo"/> it worked fine.
As per Clinton, the missing column must have been caught.