Merge pull request #2297 from FOCONIS/fix-mutable-next-changelog

FIX: Changelog-oldValue did not work for Json mutable properties
This commit is contained in:
Rob Bygrave
2021-08-10 21:10:15 +12:00
committed by GitHub
3 changed files with 55 additions and 4 deletions
@@ -395,6 +395,15 @@ public final class EntityBeanIntercept implements Serializable {
this.owner._ebean_setEmbeddedLoaded();
this.lazyLoadProperty = -1;
this.origValues = null;
// after save, transfer the mutable next values back to mutable info
if (mutableNext != null) {
for (int i = 0; i < mutableNext.length; i++) {
MutableValueNext next = mutableNext[i];
if (next != null) {
mutableInfo(i, next.info());
}
}
}
this.mutableNext = null;
for (int i = 0; i < flags.length; i++) {
flags[i] &= ~(FLAG_CHANGED_PROP | FLAG_ORIG_VALUE_SET);
@@ -1223,9 +1232,7 @@ public final class EntityBeanIntercept implements Serializable {
if (mutableNext == null) {
return null;
}
final MutableValueNext next = mutableNext[propertyIndex];
mutableInfo(propertyIndex, next.info());
return next.content();
return mutableNext[propertyIndex].content();
}
}
@@ -16,10 +16,13 @@ import io.ebean.event.changelog.ChangeLogRegister;
import io.ebean.event.changelog.ChangeSet;
import io.ebean.event.changelog.ChangeType;
import io.ebean.event.changelog.TxnState;
import io.ebeantest.LoggedSql;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.tests.model.basic.EBasicChangeLog;
import org.tests.model.json.PlainBean;
import java.util.ArrayList;
import java.util.List;
@@ -130,7 +133,31 @@ public class TestChangeLog extends BaseTestCase {
assertThat(change.getEvent()).isEqualTo(ChangeType.DELETE);
assertThat(change.getData()).isNull();
}
@Test
public void testWithJsonMutationDetection() {
EBasicChangeLog bean = new EBasicChangeLog();
bean.setName(null);
bean.setShortDescription("hello");
PlainBean jsonBean = new PlainBean();
bean.setPlainBean(jsonBean);
jsonBean.setName("A");
server.save(bean);
BeanChange change = firstChange();
assertThat(change.getEvent()).isEqualTo(ChangeType.INSERT);
jsonBean.setName("B");
LoggedSql.start();
server.save(bean);
assertThat(LoggedSql.stop()).isNotEmpty();
change = firstChange();
assertThat(change.getEvent()).isEqualTo(ChangeType.UPDATE);
assertThat(change.getData()).contains("\"plainBean\":{\"name\":\"B\"");
assertThat(change.getOldData()).contains("\"plainBean\":{\"name\":\"A\"");
}
private Database createServer() {
DatabaseConfig config = new DatabaseConfig();
@@ -2,6 +2,7 @@ package org.tests.model.basic;
import io.ebean.annotation.Cache;
import io.ebean.annotation.ChangeLog;
import io.ebean.annotation.DbJson;
import io.ebean.annotation.ReadAudit;
import io.ebean.annotation.WhenCreated;
import io.ebean.annotation.WhenModified;
@@ -12,11 +13,16 @@ import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Version;
import javax.validation.constraints.Size;
import org.tests.model.json.PlainBean;
import static io.ebean.annotation.MutationDetection.SOURCE;
import java.sql.Timestamp;
@Cache(enableQueryCache = true)
@ReadAudit
@ChangeLog(updatesThatInclude = {"name", "shortDescription"})
@ChangeLog(updatesThatInclude = {"name", "shortDescription", "plainBean"})
@Entity
public class EBasicChangeLog {
@@ -46,6 +52,9 @@ public class EBasicChangeLog {
@Version
Long version;
@DbJson(length = 500, mutationDetection = SOURCE) // such that we can rebuild old values
PlainBean plainBean;
public Long getId() {
return id;
@@ -118,4 +127,12 @@ public class EBasicChangeLog {
public void setVersion(Long version) {
this.version = version;
}
public PlainBean getPlainBean() {
return plainBean;
}
public void setPlainBean(PlainBean plainBean) {
this.plainBean = plainBean;
}
}