mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 03:22:59 +00:00
#2278 - Move null to empty json collections check to BeanPropertyJsonBasic
This commit is contained in:
@@ -625,27 +625,13 @@ public class BeanProperty implements ElPropertyValue, Property, STreeProperty {
|
||||
return scalarType.read(reader);
|
||||
}
|
||||
|
||||
protected Object checkForEmpty(EntityBean bean) {
|
||||
final Object value = getValue(bean);
|
||||
if (value instanceof Collection && ((Collection<?>) value).isEmpty()
|
||||
|| value instanceof Map && ((Map<?, ?>) value).isEmpty()) {
|
||||
return value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object readSet(DataReader reader, EntityBean bean) throws SQLException {
|
||||
try {
|
||||
Object value = scalarType.read(reader);
|
||||
if (value == null) {
|
||||
value = checkForEmpty(bean);
|
||||
}
|
||||
if (bean != null) {
|
||||
setValue(bean, value);
|
||||
}
|
||||
return value;
|
||||
} catch (TextException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new PersistenceException("Error readSet on " + descriptor + "." + name, e);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package io.ebeaninternal.server.deploy;
|
||||
|
||||
import io.ebean.bean.EntityBean;
|
||||
import io.ebean.core.type.DataReader;
|
||||
import io.ebean.text.TextException;
|
||||
import io.ebeaninternal.server.deploy.meta.DeployBeanProperty;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A DbJson property that does not use Jackson ObjectMapper.
|
||||
*/
|
||||
public class BeanPropertyJsonBasic extends BeanProperty {
|
||||
|
||||
public BeanPropertyJsonBasic(BeanDescriptor<?> descriptor, DeployBeanProperty deploy) {
|
||||
super(descriptor, deploy);
|
||||
}
|
||||
|
||||
protected BeanPropertyJsonBasic(BeanProperty source, BeanPropertyOverride override) {
|
||||
super(source, override);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BeanProperty override(BeanPropertyOverride override) {
|
||||
return new BeanPropertyJsonBasic(this, override);
|
||||
}
|
||||
|
||||
protected Object checkForEmpty(EntityBean bean) {
|
||||
final Object value = getValue(bean);
|
||||
if (value instanceof Collection && ((Collection<?>) value).isEmpty()
|
||||
|| value instanceof Map && ((Map<?, ?>) value).isEmpty()) {
|
||||
return value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object readSet(DataReader reader, EntityBean bean) throws SQLException {
|
||||
try {
|
||||
Object value = scalarType.read(reader);
|
||||
if (value == null) {
|
||||
value = checkForEmpty(bean);
|
||||
}
|
||||
if (bean != null) {
|
||||
setValue(bean, value);
|
||||
}
|
||||
return value;
|
||||
} catch (TextException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
throw new PersistenceException("Error readSet on " + descriptor + "." + name, e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,7 +18,7 @@ import java.util.Objects;
|
||||
/**
|
||||
* Handle json property with MutationDetection of SOURCE or HASH only.
|
||||
*/
|
||||
public class BeanPropertyJsonMapper extends BeanProperty {
|
||||
public class BeanPropertyJsonMapper extends BeanPropertyJsonBasic {
|
||||
|
||||
private final boolean sourceDetection;
|
||||
|
||||
|
||||
+3
-3
@@ -316,9 +316,6 @@ public class DeployBeanProperty {
|
||||
}
|
||||
|
||||
public MutationDetection getMutationDetection() {
|
||||
if (mutationDetection == null) {
|
||||
mutationDetection = MutationDetection.DEFAULT;
|
||||
}
|
||||
return mutationDetection;
|
||||
}
|
||||
|
||||
@@ -1204,4 +1201,7 @@ public class DeployBeanProperty {
|
||||
return scalarType != null && scalarType.isJsonMapper();
|
||||
}
|
||||
|
||||
boolean isJsonType() {
|
||||
return mutationDetection != null;
|
||||
}
|
||||
}
|
||||
|
||||
+3
@@ -453,6 +453,9 @@ public class DeployBeanPropertyLists {
|
||||
if (deployProp.isJsonMapper()) {
|
||||
return new BeanPropertyJsonMapper(desc, deployProp);
|
||||
}
|
||||
if (deployProp.isJsonType()) {
|
||||
return new BeanPropertyJsonBasic(desc, deployProp);
|
||||
}
|
||||
return new BeanProperty(desc, deployProp);
|
||||
}
|
||||
|
||||
|
||||
@@ -205,9 +205,6 @@ public class DeployUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This property is marked as a Lob object.
|
||||
*/
|
||||
void setDbJsonType(DeployBeanProperty prop, DbJson dbJsonType) {
|
||||
int dbType = getDbJsonStorage(dbJsonType.storage());
|
||||
setDbJsonType(prop, dbType, dbJsonType.length(), dbJsonType.mutationDetection());
|
||||
|
||||
@@ -7,6 +7,7 @@ import javax.persistence.Column;
|
||||
import javax.persistence.Embeddable;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import java.util.Map;
|
||||
|
||||
@Embeddable
|
||||
public class EAddress {
|
||||
@@ -24,6 +25,9 @@ public class EAddress {
|
||||
@DbJson
|
||||
PlainBean jbean;
|
||||
|
||||
@DbJson
|
||||
Map<String, Object> jraw;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
@@ -56,6 +60,14 @@ public class EAddress {
|
||||
this.jbean = jbean;
|
||||
}
|
||||
|
||||
public Map<String, Object> getJraw() {
|
||||
return jraw;
|
||||
}
|
||||
|
||||
public void setJraw(Map<String, Object> jraw) {
|
||||
this.jraw = jraw;
|
||||
}
|
||||
|
||||
public EAddressStatus getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ public class EBasicJsonMapVarchar {
|
||||
String name;
|
||||
|
||||
@DbJson(storage = DbJsonType.VARCHAR)//, length = 2200)
|
||||
Map<String, Object> content;
|
||||
Map<String, Object> content;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
|
||||
@@ -9,7 +9,9 @@ import org.tests.model.embedded.EAddress;
|
||||
import org.tests.model.embedded.EPerson;
|
||||
import org.tests.model.json.PlainBean;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@@ -18,17 +20,20 @@ public class TestNativeWithEmbedded extends BaseTestCase {
|
||||
@Test
|
||||
public void test() {
|
||||
|
||||
Map<String, Object> rawMap = new LinkedHashMap<>();
|
||||
rawMap.put("a","1");
|
||||
EPerson person = new EPerson();
|
||||
person.setName("Frank");
|
||||
EAddress address = new EAddress();
|
||||
address.setStreet("1 foo st");
|
||||
address.setCity("barv");
|
||||
address.setJbean(new PlainBean("hi", 3));
|
||||
address.setJraw(rawMap);
|
||||
person.setAddress(address);
|
||||
|
||||
DB.save(person);
|
||||
|
||||
String sql = "select id, name, street, suburb, addr_city, addr_status, addr_jbean from eperson where id = ?";
|
||||
String sql = "select id, name, street, suburb, addr_city, addr_status, addr_jbean, jraw from eperson where id = ?";
|
||||
|
||||
LoggedSqlCollector.start();
|
||||
|
||||
@@ -39,6 +44,7 @@ public class TestNativeWithEmbedded extends BaseTestCase {
|
||||
assertThat(one.getName()).isEqualTo("Frank");
|
||||
assertThat(one.getAddress().getStreet()).isEqualTo("1 foo st");
|
||||
assertThat(one.getAddress().getJbean().getName()).isEqualTo("hi");
|
||||
assertThat(one.getAddress().getJraw().get("a")).isEqualTo("1");
|
||||
|
||||
List<String> loggedSql = LoggedSqlCollector.stop();
|
||||
assertThat(loggedSql).hasSize(1);
|
||||
|
||||
Reference in New Issue
Block a user