diff --git a/pom.xml b/pom.xml index 0980675..555b44b 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 net.helenus helenus-core - 2.0.16-SNAPSHOT + 2.0.17-SNAPSHOT jar helenus diff --git a/src/main/java/net/helenus/core/operation/InsertOperation.java b/src/main/java/net/helenus/core/operation/InsertOperation.java index a9b4528..e6220ad 100644 --- a/src/main/java/net/helenus/core/operation/InsertOperation.java +++ b/src/main/java/net/helenus/core/operation/InsertOperation.java @@ -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 extends AbstractOperation 0) { - Map backingMap = new HashMap(values.size()); + Collection properties = entity.getOrderedProperties(); + Map backingMap = new HashMap(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; diff --git a/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java b/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java index 1f2182b..c1e55bd 100644 --- a/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java +++ b/src/main/java/net/helenus/core/reflect/MapperInvocationHandler.java @@ -86,22 +86,22 @@ public class MapperInvocationHandler 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(); - } + } } diff --git a/src/main/java/net/helenus/mapping/value/ValueProviderMap.java b/src/main/java/net/helenus/mapping/value/ValueProviderMap.java index daf5677..a29da62 100644 --- a/src/main/java/net/helenus/mapping/value/ValueProviderMap.java +++ b/src/main/java/net/helenus/mapping/value/ValueProviderMap.java @@ -74,46 +74,46 @@ public final class ValueProviderMap implements Map { @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 m) { - throwShouldNeverCall(); + throwShouldNeverCall("putAll()"); } @Override public void clear() { - throwShouldNeverCall(); + throwShouldNeverCall("clear()"); } @Override public Collection values() { - throwShouldNeverCall(); + throwShouldNeverCall("values()"); return null; } @Override public Set> 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