Skip to content

Commit

Permalink
Avoid exception when replacing mutable attributes that have no setter…
Browse files Browse the repository at this point in the history
…, always create embeddable instances when doing entity flushing and given a null object
  • Loading branch information
beikov committed Dec 18, 2018
1 parent d409e35 commit 3454c42
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1376,15 +1376,20 @@ private CtMethod addReplaceAttribute(CtClass cc, AbstractMethodAttribute[] attri
sb.append("\tswitch ($2) {");
for (int i = 0; i < attributes.length; i++) {
AbstractMethodAttribute attribute = attributes[i];
if (attribute != null && attribute.hasDirtyStateIndex() && ReflectionUtils.getSetter(attribute.getDeclaringType().getJavaType(), attribute.getName()) != null) {
sb.append("\t\tcase ").append(attribute.getDirtyStateIndex()).append(": $0.set").append(Character.toUpperCase(attribute.getName().charAt(0))).append(attribute.getName(), 1, attribute.getName().length());
sb.append('(');
if (attribute.getJavaType().isPrimitive()) {
appendUnwrap(sb, attribute.getJavaType(), "$3");
} else {
sb.append("(").append(attribute.getJavaType().getName()).append(") $3");
if (attribute != null && attribute.hasDirtyStateIndex()) {
sb.append("\t\tcase ").append(attribute.getDirtyStateIndex()).append(": ");
// If there is no setter, we simply ignore the object rather than throwing an exception
if (ReflectionUtils.getSetter(attribute.getDeclaringType().getJavaType(), attribute.getName()) != null) {
sb.append("$0.set").append(Character.toUpperCase(attribute.getName().charAt(0))).append(attribute.getName(), 1, attribute.getName().length());
sb.append('(');
if (attribute.getJavaType().isPrimitive()) {
appendUnwrap(sb, attribute.getJavaType(), "$3");
} else {
sb.append("(").append(attribute.getJavaType().getName()).append(") $3");
}
sb.append("); ");
}
sb.append("); break;\n");
sb.append("break;\n");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ public boolean flushEntity(UpdateContext context, Object entity, Object ownerVie
}
} else {
// In case of nested attributes, the entity instance we get is the container of the attribute
if (loadForEntityFlush && !entityClass.isInstance(entity)) {
if ((loadForEntityFlush || viewIdAccessor == null) && !entityClass.isInstance(entity)) {
entity = entityLoader.toEntity(context, id);
}
}
Expand Down

0 comments on commit 3454c42

Please sign in to comment.