Fix the InsertOperation transform method to properly hydrate the pojo with all property values or, in some cases, their default values before returning it to the caller.
This commit is contained in:
parent
ec55c67d17
commit
89303f9179
4 changed files with 50 additions and 21 deletions
2
pom.xml
2
pom.xml
|
@ -5,7 +5,7 @@
|
|||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>net.helenus</groupId>
|
||||
<artifactId>helenus-core</artifactId>
|
||||
<version>2.0.16-SNAPSHOT</version>
|
||||
<version>2.0.17-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>helenus</name>
|
||||
|
|
|
@ -22,6 +22,7 @@ import com.datastax.driver.core.querybuilder.QueryBuilder;
|
|||
import net.helenus.core.AbstractSessionOperations;
|
||||
import net.helenus.core.Getter;
|
||||
import net.helenus.core.Helenus;
|
||||
import net.helenus.core.reflect.DefaultPrimitiveTypes;
|
||||
import net.helenus.core.reflect.HelenusPropertyNode;
|
||||
import net.helenus.core.reflect.MapExportable;
|
||||
import net.helenus.mapping.HelenusEntity;
|
||||
|
@ -29,6 +30,7 @@ import net.helenus.mapping.HelenusProperty;
|
|||
import net.helenus.mapping.MappingUtil;
|
||||
import net.helenus.mapping.value.BeanColumnValueProvider;
|
||||
import net.helenus.support.Fun;
|
||||
import net.helenus.support.HelenusException;
|
||||
import net.helenus.support.HelenusMappingException;
|
||||
|
||||
import java.util.*;
|
||||
|
@ -142,9 +144,36 @@ public final class InsertOperation<T> extends AbstractOperation<T, InsertOperati
|
|||
return (T) pojo;
|
||||
} else {
|
||||
if (values.size() > 0) {
|
||||
Map<String, Object> backingMap = new HashMap<String, Object>(values.size());
|
||||
Collection<HelenusProperty> properties = entity.getOrderedProperties();
|
||||
Map<String, Object> backingMap = new HashMap<String, Object>(properties.size());
|
||||
|
||||
// First, add all the inserted values into our new map.
|
||||
values.forEach(t -> backingMap.put(t._1.getProperty().getPropertyName(), t._2));
|
||||
pojo = Helenus.map(values.get(0)._1.getEntity().getMappingInterface(), backingMap);
|
||||
|
||||
// Then, fill in all the rest of the properties.
|
||||
for (HelenusProperty prop : properties) {
|
||||
String key = prop.getPropertyName();
|
||||
if (!backingMap.containsKey(key)) {
|
||||
// If we started this operation with an instance of this type, use values from that.
|
||||
if (pojo != null) {
|
||||
backingMap.put(key, BeanColumnValueProvider.INSTANCE.getColumnValue(pojo, -1, prop));
|
||||
} else {
|
||||
// Otherwise we'll use default values for the property type if available.
|
||||
Class<?> propType = prop.getJavaType();
|
||||
if (propType.isPrimitive()) {
|
||||
DefaultPrimitiveTypes type = DefaultPrimitiveTypes.lookup(propType);
|
||||
if (type == null) {
|
||||
throw new HelenusException("unknown primitive type " + propType);
|
||||
}
|
||||
backingMap.put(key, type.getDefaultValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Lastly, create a new proxy object for the entity and return the new instance.
|
||||
Class<?> iface = entity.getMappingInterface();
|
||||
pojo = Helenus.map(iface, backingMap);
|
||||
}
|
||||
}
|
||||
return (T) pojo;
|
||||
|
|
|
@ -86,22 +86,22 @@ public class MapperInvocationHandler<E> implements InvocationHandler {
|
|||
return Collections.unmodifiableMap(src);
|
||||
}
|
||||
|
||||
Object value = src.get(methodName);
|
||||
Object value = src.get(methodName);
|
||||
|
||||
Class<?> returnType = method.getReturnType();
|
||||
|
||||
if (value == null) {
|
||||
if (value == null) {
|
||||
|
||||
if (returnType.isPrimitive()) {
|
||||
if (returnType.isPrimitive()) {
|
||||
|
||||
DefaultPrimitiveTypes type = DefaultPrimitiveTypes.lookup(returnType);
|
||||
if (type == null) {
|
||||
throw new HelenusException("unknown primitive type " + returnType);
|
||||
}
|
||||
DefaultPrimitiveTypes type = DefaultPrimitiveTypes.lookup(returnType);
|
||||
if (type == null) {
|
||||
throw new HelenusException("unknown primitive type " + returnType);
|
||||
}
|
||||
|
||||
return type.getDefaultValue();
|
||||
return type.getDefaultValue();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -74,46 +74,46 @@ public final class ValueProviderMap implements Map<String, Object> {
|
|||
|
||||
@Override
|
||||
public boolean containsValue(Object value) {
|
||||
throwShouldNeverCall();
|
||||
throwShouldNeverCall("containsValue()");
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object put(String key, Object value) {
|
||||
throwShouldNeverCall();
|
||||
throwShouldNeverCall("put()");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object remove(Object key) {
|
||||
throwShouldNeverCall();
|
||||
throwShouldNeverCall("remove()");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void putAll(Map<? extends String, ? extends Object> m) {
|
||||
throwShouldNeverCall();
|
||||
throwShouldNeverCall("putAll()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
throwShouldNeverCall();
|
||||
throwShouldNeverCall("clear()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<Object> values() {
|
||||
throwShouldNeverCall();
|
||||
throwShouldNeverCall("values()");
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<java.util.Map.Entry<String, Object>> entrySet() {
|
||||
throwShouldNeverCall();
|
||||
throwShouldNeverCall("entrySet()");
|
||||
return null;
|
||||
}
|
||||
|
||||
private void throwShouldNeverCall() {
|
||||
throw new HelenusMappingException("should never be called");
|
||||
private void throwShouldNeverCall(String methodName) {
|
||||
throw new HelenusMappingException(String.format("the method {} should never be called on an instance of a Helenus ValueProviderMap", methodName));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue