Skip to content

Commit c8aa246

Browse files
committed
Ensure setters are called only once
1 parent 93c84f4 commit c8aa246

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,16 @@ private boolean applyNestedResultMappings(ResultSet rs, ResultMap resultMap, Met
158158
final boolean isAncestor = ancestorCache.containsKey(absoluteKey);
159159
Object rowValue = getRowValue(rs, nestedResultMap, combinedKey, absoluteKey, columnPrefix, resultColumnCache);
160160
final Object collectionProperty = instantiateCollectionPropertyIfAppropriate(resultMapping, metaObject); // even if there is no data an empty collection is set
161-
if (!knownValue && rowValue != null && anyNotNullColumnHasValue(resultMapping, columnPrefix, rs)) {
161+
if (rowValue != null
162+
&& ((isAncestor && parentIsNew)
163+
|| (!isAncestor && !knownValue && anyNotNullColumnHasValue(resultMapping, columnPrefix, rs)))) {
162164
if (collectionProperty != null) {
163165
final MetaObject targetMetaObject = configuration.newMetaObject(collectionProperty);
164166
targetMetaObject.add(rowValue);
165167
} else {
166-
if (!parentIsNew && !isAncestor) {
167-
throw new ExecutorException("Trying to set a 1 to 1 child element twice for '" + resultMapping.getProperty() + "'. Check your id properties.");
168+
if (!parentIsNew) {
169+
throw new ExecutorException("Trying to orverride a previous set value for the association '" + resultMapping.getProperty()
170+
+ "'. Check your id/result elements to ensure they identify uniquely an record.");
168171
}
169172
metaObject.setValue(resultMapping.getProperty(), rowValue);
170173
}

src/test/java/org/apache/ibatis/submitted/parent_reference_3level/Blog.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public int getId() {
3030
return id;
3131
}
3232

33-
public void setId(int id) {
33+
public void setId(int id) {
3434
this.id = id;
3535
}
3636

@@ -39,6 +39,9 @@ public String getTitle() {
3939
}
4040

4141
public void setTitle(String title) {
42+
if (this.title != null) {
43+
throw new RuntimeException("Setter called twice");
44+
}
4245
this.title = title;
4346
}
4447

@@ -47,6 +50,9 @@ public List<Post> getPosts() {
4750
}
4851

4952
public void setPosts(List<Post> posts) {
53+
if (this.posts != null) {
54+
throw new RuntimeException("Setter called twice");
55+
}
5056
this.posts = posts;
5157
}
5258
}

src/test/java/org/apache/ibatis/submitted/parent_reference_3level/Comment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public Post getPost() {
3434
}
3535

3636
public void setPost(Post post) {
37+
if (this.post != null) {
38+
throw new RuntimeException("Setter called twice");
39+
}
3740
this.post = post;
3841
}
3942

@@ -42,6 +45,9 @@ public String getComment() {
4245
}
4346

4447
public void setComment(String comment) {
48+
if (this.comment != null) {
49+
throw new RuntimeException("Setter called twice");
50+
}
4551
this.comment = comment;
4652
}
4753
}

src/test/java/org/apache/ibatis/submitted/parent_reference_3level/Post.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public Blog getBlog() {
3737
}
3838

3939
public void setBlog(Blog blog) {
40+
if (this.blog != null) {
41+
throw new RuntimeException("Setter called twice");
42+
}
4043
this.blog = blog;
4144
}
4245

@@ -45,6 +48,9 @@ public String getBody() {
4548
}
4649

4750
public void setBody(String body) {
51+
if (this.body != null) {
52+
throw new RuntimeException("Setter called twice");
53+
}
4854
this.body = body;
4955
}
5056

@@ -53,6 +59,9 @@ public List<Comment> getComments() {
5359
}
5460

5561
public void setComments(List<Comment> comments) {
62+
if (this.comments != null) {
63+
throw new RuntimeException("Setter called twice");
64+
}
5665
this.comments = comments;
5766
}
5867
}

0 commit comments

Comments
 (0)