#1785 - Bean gets replaced on json deserialization with primitive 'null' id

This commit is contained in:
rob bygrave
2019-08-12 21:05:32 +12:00
parent ee6452cfb0
commit f5459920e9
7 changed files with 45 additions and 11 deletions
@@ -112,6 +112,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import static io.ebeaninternal.server.persist.DmlUtil.isNullOrZero;
/**
* Describes Beans including their deployment information.
*/
@@ -3210,7 +3212,7 @@ public class BeanDescriptor<T> implements BeanType<T>, STreeType {
}
boolean hasIdValue(EntityBean bean) {
return (idProperty != null && !DmlUtil.isNullOrZero(idProperty.getValue(bean)));
return (idProperty != null && !isNullOrZero(idProperty.getValue(bean)));
}
boolean hasVersionProperty(EntityBeanIntercept ebi) {
@@ -14,6 +14,8 @@ import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;
import static io.ebeaninternal.server.persist.DmlUtil.isNullOrZero;
class BeanDescriptorJsonHelp<T> {
private final BeanDescriptor<T> desc;
@@ -158,7 +160,7 @@ class BeanDescriptorJsonHelp<T> {
}
Object contextBean = null;
Object id = desc.beanId(bean);
if (id != null) {
if (!isNullOrZero(id)) {
// check if the bean has already been loaded
contextBean = readJson.persistenceContextPutIfAbsent(id, bean, desc);
}
@@ -43,6 +43,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static io.ebeaninternal.server.persist.DmlUtil.isNullOrZero;
/**
* Persister implementation using DML.
* <p>
@@ -1227,11 +1229,9 @@ public final class DefaultPersister implements Persister {
EntityBean bean = request.getEntityBean();
Object uid = idProp.getValue(bean);
if (DmlUtil.isNullOrZero(uid)) {
if (isNullOrZero(uid)) {
// generate the nextId and set it to the property
Object nextId = desc.nextId(request.getTransaction());
// cast the data type if required and set it
desc.convertSetId(nextId, bean);
}
@@ -20,6 +20,8 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import static io.ebeaninternal.server.persist.DmlUtil.isNullOrZero;
/**
* Saves the details for a OneToMany or ManyToMany relationship (entity beans).
*/
@@ -222,7 +224,7 @@ public class SaveManyBeans extends SaveManyBase {
}
if (detailBean instanceof EntityBean) {
Object id = targetDescriptor.getId((EntityBean) detailBean);
if (!DmlUtil.isNullOrZero(id)) {
if (!isNullOrZero(id)) {
// remember the Id (other details not in the collection) will be removed
detailIds.add(id);
}
@@ -6,7 +6,6 @@ import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.core.Message;
import io.ebeaninternal.server.core.PersistRequestBean;
import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.persist.DmlUtil;
import javax.persistence.OptimisticLockException;
import javax.persistence.PersistenceException;
@@ -15,6 +14,8 @@ import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import static io.ebeaninternal.server.persist.DmlUtil.isNullOrZero;
/**
* Insert bean handler.
*/
@@ -66,7 +67,7 @@ public class InsertHandler extends DmlHandler {
Object idValue = desc.getId(bean);
boolean withId = !DmlUtil.isNullOrZero(idValue);
boolean withId = !isNullOrZero(idValue);
// check to see if we are going to use generated keys
if (!withId) {
@@ -10,7 +10,7 @@ import javax.persistence.Version;
public class RelMaster {
@Id
Long id;
long id;
String name;
@@ -20,11 +20,11 @@ public class RelMaster {
@ManyToOne(cascade = CascadeType.REMOVE)
private RelDetail detail;
public Long getId() {
public long getId() {
return id;
}
public void setId(Long id) {
public void setId(long id) {
this.id = id;
}
@@ -0,0 +1,27 @@
package org.tests.cascade;
import io.ebean.DB;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class TestJsonMarshallingOTM {
@Test
public void test() {
String json = ("" +
"{ 'masterRel': [" +
" { 'name': 'a' }," +
" { 'name': 'b' }" +
"] }"
).replaceAll("'", "\"");
RelDetail bean = DB.json().toBean(RelDetail.class, json);
final RelMaster m0 = bean.getMasterRel().get(0);
assertThat(m0.getName()).isEqualTo("a");
final RelMaster m1 = bean.getMasterRel().get(1);
assertThat(m1.getName()).isEqualTo("b");
}
}