Skip to content

Commit d279139

Browse files
committed
Fix for http://code.google.com/p/mybatis/issues/detail?id=393 (add resultMap attribute to constructor/idArg element)
1 parent 400d128 commit d279139

File tree

6 files changed

+160
-0
lines changed

6 files changed

+160
-0
lines changed

pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
<name>Riccardo Cossu</name>
4747
<email>[email protected]</email>
4848
</contributor>
49+
<contributor>
50+
<name>Tim Chen</name>
51+
<email>[email protected]</email>
52+
</contributor>
4953
</contributors>
5054
<properties>
5155
<findbugs.onlyAnalyze>org.apache.ibatis.*</findbugs.onlyAnalyze>

src/main/java/org/apache/ibatis/builder/xml/mybatis-3-mapper.dtd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ javaType CDATA #IMPLIED
7070
column CDATA #IMPLIED
7171
jdbcType CDATA #IMPLIED
7272
typeHandler CDATA #IMPLIED
73+
select CDATA #IMPLIED
74+
resultMap CDATA #IMPLIED
7375
>
7476

7577
<!ELEMENT arg EMPTY>
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package domain.blog;
2+
3+
import java.io.Serializable;
4+
5+
public class ComplexImmutableAuthor implements Serializable {
6+
private final ComplexImmutableAuthorId theComplexImmutableAuthorId;
7+
protected final String bio;
8+
protected final Section favouriteSection;
9+
10+
public ComplexImmutableAuthor(ComplexImmutableAuthorId aComplexImmutableAuthorId, String bio, Section section) {
11+
theComplexImmutableAuthorId = aComplexImmutableAuthorId;
12+
this.bio = bio;
13+
this.favouriteSection = section;
14+
}
15+
16+
public ComplexImmutableAuthorId getComplexImmutableAuthorId() {
17+
return theComplexImmutableAuthorId;
18+
}
19+
20+
public String getBio() {
21+
return bio;
22+
}
23+
24+
public Section getFavouriteSection() {
25+
return favouriteSection;
26+
}
27+
28+
@Override
29+
public boolean equals(Object o) {
30+
if (this == o) {
31+
return true;
32+
}
33+
if (o == null || getClass() != o.getClass()) {
34+
return false;
35+
}
36+
37+
final ComplexImmutableAuthor that = (ComplexImmutableAuthor) o;
38+
39+
if (bio != null ? !bio.equals(that.bio) : that.bio != null) {
40+
return false;
41+
}
42+
if (favouriteSection != that.favouriteSection) {
43+
return false;
44+
}
45+
if (theComplexImmutableAuthorId != null ? !theComplexImmutableAuthorId.equals(that.theComplexImmutableAuthorId) : that.theComplexImmutableAuthorId != null) {
46+
return false;
47+
}
48+
49+
return true;
50+
}
51+
52+
@Override
53+
public int hashCode() {
54+
int myresult = theComplexImmutableAuthorId != null ? theComplexImmutableAuthorId.hashCode() : 0;
55+
myresult = 31 * myresult + (bio != null ? bio.hashCode() : 0);
56+
myresult = 31 * myresult + (favouriteSection != null ? favouriteSection.hashCode() : 0);
57+
return myresult;
58+
}
59+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package domain.blog;
2+
3+
public class ComplexImmutableAuthorId {
4+
protected final int id;
5+
protected final String email;
6+
protected final String username;
7+
protected final String password;
8+
9+
public ComplexImmutableAuthorId(int aId, String aEmail, String aUsername, String aPassword) {
10+
id = aId;
11+
email = aEmail;
12+
username = aUsername;
13+
password = aPassword;
14+
}
15+
16+
public int getId() {
17+
return id;
18+
}
19+
20+
public String getEmail() {
21+
return email;
22+
}
23+
24+
public String getUsername() {
25+
return username;
26+
}
27+
28+
public String getPassword() {
29+
return password;
30+
}
31+
32+
@Override
33+
public boolean equals(Object o) {
34+
if (this == o) {
35+
return true;
36+
}
37+
if (o == null || getClass() != o.getClass()) {
38+
return false;
39+
}
40+
41+
final ComplexImmutableAuthorId that = (ComplexImmutableAuthorId) o;
42+
43+
if (id != that.id) {
44+
return false;
45+
}
46+
if (email != null ? !email.equals(that.email) : that.email != null) {
47+
return false;
48+
}
49+
if (password != null ? !password.equals(that.password) : that.password != null) {
50+
return false;
51+
}
52+
if (username != null ? !username.equals(that.username) : that.username != null) {
53+
return false;
54+
}
55+
56+
return true;
57+
}
58+
59+
@Override
60+
public int hashCode() {
61+
int myresult = id;
62+
myresult = 31 * myresult + (email != null ? email.hashCode() : 0);
63+
myresult = 31 * myresult + (username != null ? username.hashCode() : 0);
64+
myresult = 31 * myresult + (password != null ? password.hashCode() : 0);
65+
return myresult;
66+
}
67+
}

src/test/java/org/apache/ibatis/builder/AuthorMapper.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,33 @@
3131
</constructor>
3232
</resultMap>
3333

34+
<resultMap id="complexAuthorId" type="domain.blog.ComplexImmutableAuthorId">
35+
<constructor>
36+
<idArg column="id" javaType="_int"/>
37+
<idArg column="username" javaType="string"/>
38+
<idArg column="password" javaType="string"/>
39+
<idArg column="email" javaType="string"/>
40+
</constructor>
41+
</resultMap>
42+
43+
<resultMap id="selectComplexImmutableAuthor" type="domain.blog.ComplexImmutableAuthor">
44+
<constructor>
45+
<idArg javaType="domain.blog.ComplexImmutableAuthorId" resultMap="complexAuthorId"/>
46+
<arg column="bio" javaType="string"/>
47+
<arg column="favourite_section" javaType="domain.blog.Section"/>
48+
</constructor>
49+
</resultMap>
50+
3451
<select id="selectAllAuthors"
3552
resultType="domain.blog.Author">
3653
select * from author
3754
</select>
3855

56+
<select id="selectComplexAuthors"
57+
resultMap="selectComplexImmutableAuthor">
58+
select * from author
59+
</select>
60+
3961
<select id="selectAuthor"
4062
parameterMap="selectAuthor"
4163
resultMap="selectAuthor">

src/test/java/org/apache/ibatis/session/SqlSessionManagerTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@ public void shouldSelectAllAuthors() throws Exception {
103103
assertEquals(2, authors.size());
104104
}
105105

106+
@Test
107+
public void shouldSelectAllComplexAuthors() throws Exception {
108+
List<ComplexImmutableAuthor> authors = manager.selectList("domain.blog.mappers.AuthorMapper.selectComplexAuthors");
109+
assertEquals(2, authors.size());
110+
}
111+
106112
@Test
107113
public void shouldSelectCountOfPosts() throws Exception {
108114
Integer count = (Integer) manager.selectOne("domain.blog.mappers.BlogMapper.selectCountOfPosts");

0 commit comments

Comments
 (0)