FIX: Changelog-oldValue did not work for Json mutable properties

This commit is contained in:
Roland Praml committed 2021-08-10 09:30:25 +02:00
1 parent 54e8c4df8a
commit c8ae3c5ef6
3 files changed
+55 -4

No files matched your search

@@ -395,6 +395,15 @@ public final class EntityBeanIntercept implements Serializable {
this.owner._ebean_setEmbeddedLoaded(); this.owner._ebean_setEmbeddedLoaded();
this.lazyLoadProperty = -1; this.lazyLoadProperty = -1;
this.origValues = null; 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; this.mutableNext = null;
for (int i = 0; i < flags.length; i++) { for (int i = 0; i < flags.length; i++) {
flags[i] &= ~(FLAG_CHANGED_PROP | FLAG_ORIG_VALUE_SET); flags[i] &= ~(FLAG_CHANGED_PROP | FLAG_ORIG_VALUE_SET);
@@ -1223,9 +1232,7 @@ public final class EntityBeanIntercept implements Serializable {
if (mutableNext == null) { if (mutableNext == null) {
return null; return null;
} }
final MutableValueNext next = mutableNext[propertyIndex]; return mutableNext[propertyIndex].content();
mutableInfo(propertyIndex, next.info());
return next.content();
} }
} }
@@ -16,10 +16,13 @@ import io.ebean.event.changelog.ChangeLogRegister;
import io.ebean.event.changelog.ChangeSet; import io.ebean.event.changelog.ChangeSet;
import io.ebean.event.changelog.ChangeType; import io.ebean.event.changelog.ChangeType;
import io.ebean.event.changelog.TxnState; import io.ebean.event.changelog.TxnState;
import io.ebeantest.LoggedSql;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.tests.model.basic.EBasicChangeLog; import org.tests.model.basic.EBasicChangeLog;
import org.tests.model.json.PlainBean;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -130,7 +133,31 @@ public class TestChangeLog extends BaseTestCase {
assertThat(change.getEvent()).isEqualTo(ChangeType.DELETE); assertThat(change.getEvent()).isEqualTo(ChangeType.DELETE);
assertThat(change.getData()).isNull(); 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() { private Database createServer() {
DatabaseConfig config = new DatabaseConfig(); DatabaseConfig config = new DatabaseConfig();
@@ -2,6 +2,7 @@ package org.tests.model.basic;
import io.ebean.annotation.Cache; import io.ebean.annotation.Cache;
import io.ebean.annotation.ChangeLog; import io.ebean.annotation.ChangeLog;
import io.ebean.annotation.DbJson;
import io.ebean.annotation.ReadAudit; import io.ebean.annotation.ReadAudit;
import io.ebean.annotation.WhenCreated; import io.ebean.annotation.WhenCreated;
import io.ebean.annotation.WhenModified; import io.ebean.annotation.WhenModified;
@@ -12,11 +13,16 @@ import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.Version; import javax.persistence.Version;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import org.tests.model.json.PlainBean;
import static io.ebean.annotation.MutationDetection.SOURCE;
import java.sql.Timestamp; import java.sql.Timestamp;
@Cache(enableQueryCache = true) @Cache(enableQueryCache = true)
@ReadAudit @ReadAudit
@ChangeLog(updatesThatInclude = {"name", "shortDescription"}) @ChangeLog(updatesThatInclude = {"name", "shortDescription", "plainBean"})
@Entity @Entity
public class EBasicChangeLog { public class EBasicChangeLog {
@@ -46,6 +52,9 @@ public class EBasicChangeLog {
@Version @Version
Long version; Long version;
@DbJson(length = 500, mutationDetection = SOURCE) // such that we can rebuild old values
PlainBean plainBean;
public Long getId() { public Long getId() {
return id; return id;
@@ -118,4 +127,12 @@ public class EBasicChangeLog {
public void setVersion(Long version) { public void setVersion(Long version) {
this.version = version; this.version = version;
} }
public PlainBean getPlainBean() {
return plainBean;
}
public void setPlainBean(PlainBean plainBean) {
this.plainBean = plainBean;
}
} }