Skip to content

Commit 907929a

Browse files
committed
Test case for issue #1848
1 parent a19b2cb commit 907929a

File tree

4 files changed

+75
-14
lines changed

4 files changed

+75
-14
lines changed

src/test/java/org/apache/ibatis/submitted/parent_childs/Child.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,27 @@
1717

1818
public class Child {
1919

20-
private int id;
20+
private Integer id;
2121
private String name;
2222
private String surName;
23-
private int age;
23+
private Integer age;
2424

25-
public int getId() {
25+
public Child() {
26+
27+
}
28+
29+
public Child(Integer id, String name, String surName, Integer age) {
30+
this.id = id;
31+
this.name = name;
32+
this.surName = surName;
33+
this.age = age;
34+
}
35+
36+
public Integer getId() {
2637
return id;
2738
}
2839

29-
public void setId(int id) {
40+
public void setId(Integer id) {
3041
this.id = id;
3142
}
3243

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
public interface Mapper {
2121

2222
List<Parent> getParents();
23+
List<Parent> getParentsWithConstructor();
2324

2425
}

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@
2323
<mapper namespace="org.apache.ibatis.submitted.parent_childs.Mapper">
2424

2525
<resultMap id="ParentMap" type="org.apache.ibatis.submitted.parent_childs.Parent">
26-
<id column="p_Id" property="id" />
27-
<id column="p_Name" property="name" />
28-
<id column="p_SurName" property="surName" />
26+
<result column="p_Id" property="id" />
27+
<result column="p_Name" property="name" />
28+
<result column="p_SurName" property="surName" />
2929
<collection property="childs" resultMap="ChildMap" />
3030
</resultMap>
3131

3232
<resultMap id="ChildMap" type="org.apache.ibatis.submitted.parent_childs.Child">
33-
<id column="c_id" property="id" />
34-
<id column="c_Name" property="name" />
35-
<id column="c_SurName" property="surName" />
36-
<id column="c_Age" property="age" />
33+
<result column="c_id" property="id" />
34+
<result column="c_Name" property="name" />
35+
<result column="c_SurName" property="surName" />
36+
<result column="c_Age" property="age" />
3737
</resultMap>
3838

3939
<select id="getParents" resultMap="ParentMap">
@@ -48,4 +48,34 @@
4848
from Parent p
4949
left outer join Child c on p.Id = c.ParentId
5050
</select>
51+
52+
<resultMap id="ParentMapConstructor" type="org.apache.ibatis.submitted.parent_childs.Parent">
53+
<result column="p_Id" property="id" />
54+
<result column="p_Name" property="name" />
55+
<result column="p_SurName" property="surName" />
56+
<collection property="childs" resultMap="ChildMapConstructor" />
57+
</resultMap>
58+
59+
<resultMap id="ChildMapConstructor" type="org.apache.ibatis.submitted.parent_childs.Child">
60+
<constructor>
61+
<arg column="c_id" javaType="int"/>
62+
<arg column="c_Name" javaType="String"/>
63+
<arg column="c_SurName" javaType="String"/>
64+
<arg column="c_Age" javaType="int"/>
65+
</constructor>
66+
</resultMap>
67+
68+
<select id="getParentsWithConstructor" resultMap="ParentMapConstructor">
69+
select
70+
p.Id as p_Id,
71+
p.Name as p_Name,
72+
p.SurName as p_SurName,
73+
c.Id as c_Id,
74+
c.Name as c_Name,
75+
c.SurName as c_SurName,
76+
c.Age as c_Age
77+
from Parent p
78+
left outer join Child c on p.Id = c.ParentId
79+
</select>
80+
5181
</mapper>

src/test/java/org/apache/ibatis/submitted/parent_childs/ParentChildTest.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.ibatis.session.SqlSession;
2424
import org.apache.ibatis.session.SqlSessionFactory;
2525
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
26+
import org.junit.Ignore;
2627
import org.junit.jupiter.api.Assertions;
2728
import org.junit.jupiter.api.BeforeAll;
2829
import org.junit.jupiter.api.Test;
@@ -34,17 +35,18 @@ class ParentChildTest {
3435
@BeforeAll
3536
static void setUp() throws Exception {
3637
// create a SqlSessionFactory
37-
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/parent_childs/mybatis-config.xml")) {
38+
try (
39+
Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/parent_childs/mybatis-config.xml")) {
3840
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
3941
}
4042

4143
// populate in-memory database
4244
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
43-
"org/apache/ibatis/submitted/parent_childs/CreateDB.sql");
45+
"org/apache/ibatis/submitted/parent_childs/CreateDB.sql");
4446
}
4547

4648
@Test
47-
void shouldGetAUser() {
49+
void shouldGet2Parents() {
4850
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
4951
Mapper mapper = sqlSession.getMapper(Mapper.class);
5052
List<Parent> parents = mapper.getParents();
@@ -58,4 +60,21 @@ void shouldGetAUser() {
5860
}
5961
}
6062

63+
// issue #1848
64+
@Test
65+
@Ignore
66+
void shouldGet2ParentsWithConstructor() {
67+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
68+
Mapper mapper = sqlSession.getMapper(Mapper.class);
69+
List<Parent> parents = mapper.getParentsWithConstructor();
70+
Assertions.assertEquals(2, parents.size());
71+
Parent firstParent = parents.get(0);
72+
Assertions.assertEquals("Jose", firstParent.getName());
73+
Assertions.assertEquals(2, firstParent.getChilds().size());
74+
Parent secondParent = parents.get(1);
75+
Assertions.assertEquals("Juan", secondParent.getName());
76+
Assertions.assertEquals(0, secondParent.getChilds().size()); // note an empty list is inyected
77+
}
78+
}
79+
6180
}

0 commit comments

Comments
 (0)