diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/DetachedEntityState.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/DetachedEntityState.java index 977d94c4..e7d1498e 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/DetachedEntityState.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/DetachedEntityState.java @@ -43,8 +43,8 @@ public class DetachedEntityState, STATE> imple private final Map dirty = new HashMap(); protected final EntityState delegate; private final static Log log = LogFactory.getLog(DetachedEntityState.class); - private GraphDatabaseContext graphDatabaseContext; - private Neo4JPersistentEntity persistentEntity; + private final GraphDatabaseContext graphDatabaseContext; + private final Neo4JPersistentEntity persistentEntity; public DetachedEntityState(final EntityState delegate, GraphDatabaseContext graphDatabaseContext) { this.delegate = delegate; @@ -134,7 +134,13 @@ private boolean mustCheckConcurrentModification() { } @Override public Object setValue(final Field field, final Object newVal) { - return setValue(property(field),newVal); + Neo4JPersistentProperty property = property(field); + if (property == null) { + // The property maybe null if it is a transient field + return newVal; + } + + return setValue(property, newVal); } private Neo4JPersistentProperty property(Field field) { @@ -146,7 +152,6 @@ public Object setValue(final Neo4JPersistentProperty property, final Object newV if (isDetached()) { final Field field = property.getField(); if (!isDirty(field) && isWritable(field)) { - Object existingValue; if (hasPersistentState()) { addDirty(field, unwrap(delegate.getValue(field)), true); } diff --git a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/OneToNRelationshipEntityFieldAccessorFactory.java b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/OneToNRelationshipEntityFieldAccessorFactory.java index 6514a1e5..7d8d5225 100644 --- a/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/OneToNRelationshipEntityFieldAccessorFactory.java +++ b/spring-data-neo4j/src/main/java/org/springframework/data/neo4j/fieldaccess/OneToNRelationshipEntityFieldAccessorFactory.java @@ -30,6 +30,11 @@ import org.springframework.data.neo4j.support.GraphBackedEntityIterableWrapper; import org.springframework.data.neo4j.support.GraphDatabaseContext; +import java.util.HashSet; +import java.util.Set; + +import static org.springframework.data.neo4j.support.DoReturn.doReturn; + public class OneToNRelationshipEntityFieldAccessorFactory implements FieldAccessorFactory { private GraphDatabaseContext graphDatabaseContext; diff --git a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/support/PropertyTest.java b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/support/PropertyTest.java index 74ca718f..befa989c 100644 --- a/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/support/PropertyTest.java +++ b/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/support/PropertyTest.java @@ -70,7 +70,13 @@ public void testGetPropertyEnum() { p.getPersistentState().setProperty("personality", "EXTROVERT"); assertEquals("Did not deserialize property value properly.", Personality.EXTROVERT, p.getPersonality()); } - + @Test + @Transactional + public void testSetTransientPropertyFieldInDetachedMode() { + Person p = new Person("Michael", 35); + p.setThought("food"); // caused a NullPointerException + } + @Test(expected = NotFoundException.class) @Transactional public void testSetTransientPropertyFieldNotManaged() {