#1990 - NPE with @Cache and setting naturalKey to null

This commit is contained in:
rob bygrave
2020-04-14 20:57:42 +12:00
parent f06f99dfc3
commit 6d43883f77
3 changed files with 48 additions and 2 deletions
@@ -1399,7 +1399,8 @@ public final class PersistRequestBean<T> extends PersistRequest implements BeanP
changes.put(property.getName(), val);
if (property.isNaturalKey()) {
updateNaturalKey = true;
changeSet.addNaturalKeyPut(beanDescriptor, key, val.toString());
String valStr = (val == null) ? null : val.toString();
changeSet.addNaturalKeyPut(beanDescriptor, key, valStr);
}
}
}
+5 -1
View File
@@ -10,7 +10,7 @@ import javax.persistence.UniqueConstraint;
@UniqueConstraint(columnNames = "app_name")
public class OCachedApp extends OCacheBase {
private final String appName;
private String appName;
public OCachedApp(String appName) {
this.appName = appName;
@@ -19,4 +19,8 @@ public class OCachedApp extends OCacheBase {
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
}
@@ -0,0 +1,41 @@
package org.tests.model.basic.cache;
import io.ebean.BaseTestCase;
import io.ebean.DB;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class TestNatKeyCacheWhenNull extends BaseTestCase {
public static final String WILL_CHANGE_TO_NULL = "WillChangeToNull";
@Test
public void updateToNull() {
OCachedApp app = setup();
// act
app.setAppName(null);
app.save();
final OCachedApp foundAfter = DB.find(OCachedApp.class)
.where().eq("appName", WILL_CHANGE_TO_NULL)
.findOne();
assertThat(foundAfter).isNull();
app.delete();
}
private OCachedApp setup() {
OCachedApp app = new OCachedApp(WILL_CHANGE_TO_NULL);
app.save();
final OCachedApp foundBefore = DB.find(OCachedApp.class)
.where().eq("appName", WILL_CHANGE_TO_NULL)
.findOne();
assertThat(foundBefore).isNotNull();
return app;
}
}