All right , this is an issue of mybatis, see
https://github.com/mybatis/mybatis-3/pull/22
and
Question
I use mybatis to query nested object
my bean, Goods.java
public class Goods {
private Integer goodsId;
private String goodsName;
private Integer goodsStorageNum;
private Integer goodsScore;
private GoodsStatus goodsStatus;
private String goodsDescription;
private List<GoodsImg> goodsImgList;
getter and setter here...
}
GoodsImg.java
public class Goods {
private Integer goodsId;
private String goodsName;
private Integer goodsStorageNum;
private Integer goodsScore;
private GoodsStatus goodsStatus;
private String goodsDescription;
private List<GoodsImg> goodsImgList;
getter and setter ...
}
two result map
<!-- goods resultmap -->
<resultMap id="goodsResultMap" type="com.qunar.scoresystem.bean.Goods">
<id property="goodsId" column="goods_id" />
<result property="goodsName" column="goods_name" />
<result property="goodsStorageNum" column="goods_storage_num" />
<result property="goodsScore" column="goods_score" />
<result property="goodsDescription" column="goods_description" />
<result property="goodsStatus" column="goods_status" />
<collection property="goodsImgList" column="goods_id" resultMap="goodsImgResult" />
</resultMap>
<!-- goodsimage resultmap -->
<resultMap id="goodsImgResult" type="com.qunar.scoresystem.bean.GoodsImg">
<id property="imgId" column="img_id" />
<result property="goodsId" column="goods_id" />
<result property="imgDir" column="img_dir" />
<result property="imgSize" column="img_size" />
<result property="imgName" column="img_name" />
</resultMap>
my sql
select goods.goods_id as goods_id, goods.goods_name as goods_name,
goods.goods_storage_num as goods_storage_num, goods.goods_score as goods_score,
goods.goods_description as goods_description, goods.goods_status as goods_status ,
goods_img.img_name as img_name , goods_img.img_dir as img_dir , goods_img.img_size as
img_size
from goods
join goods_img on goods.goods_id=goods_img.goods_id limit 10;
result of the sql in mysql
the first column below is "goods_id"
1 test_name 1 1 update desc 1 img1 tttt 1
1 test_name 1 1 update desc 1 img2 ttt 2
1 test_name 1 1 update desc 1 img3 ttt 3
2 test_name2 2 2 test desc2 0 df ttt 3
1 test_name 1 1 update desc 1 dfdf ddd 2
3 3333 3 3 ddfd 1 dfd sadf 1
1 test_name 1 1 update desc 1 sdfsd sdf 2
and the query result in java is:
List [ [Goods(id=1) with 5 GoodsImg], [Goods(id=2) with 5 GoodsImg], [Goods(id=3) with 5 GoodsImg] ]
this list contains 3 Goods all the same
and the correct list should be:
List [ [Goods(id=1) with 5 GoodsImg], [Goods(id=2) with 1 GoodsImg], [Goods(id=1) with 1 GoodsImg] ]
and, if I change the data order in mysql like this :
1 test_name 1 1 update desc 1 img1 tttt 1
1 test_name 1 1 update desc 1 img2 ttt 2
1 test_name 1 1 update desc 1 img3 ttt 3
2 test_name2 2 2 test desc2 0 df ttt 3
1 test_name 1 1 update desc 1 dfdf ddd 2
1 test_name 1 1 update desc 1 sdfsd sdf 2
3 3333 3 3 ddfd 1 dfd sadf 1
then the query result in java is:
List [ [Goods(id=1) with 5 GoodsImg], [Goods(id=1) with 5 GoodsImg], [Goods(id=3) with 1 GoodsImg] ]
still worong list, there is no Goods(id=2)
and if I change the data order in mysql like this:
1 test_name 1 1 update desc 1 img1 tttt 1
1 test_name 1 1 update desc 1 img2 ttt 2
1 test_name 1 1 update desc 1 img3 ttt 3
1 test_name 1 1 update desc 1 dfdf ddd 2
1 test_name 1 1 update desc 1 sdfsd sdf 2
2 test_name2 2 2 test desc2 0 df ttt 3
3 3333 3 3 ddfd 1 dfd sadf 1
then the query result in java is all right
since I can't control the data order in mysql, it will always return the wrong list result
so, what's the problem, is that a bug of mybatis ? my
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.0</version>
</dependency>
Thx in advance
Solution 2
OTHER TIPS
Modify join
to left join
in select statement will be working.