Skip to content
This repository has been archived by the owner on Jan 18, 2022. It is now read-only.

fixes NullPointerException with entities using transient fields in detached mode #12

Closed
wants to merge 6 commits into from
Closed
Expand Up @@ -43,8 +43,8 @@ public class DetachedEntityState<ENTITY extends GraphBacked<STATE>, STATE> imple
private final Map<Field, ExistingValue> dirty = new HashMap<Field, ExistingValue>();
protected final EntityState<ENTITY,STATE> delegate;
private final static Log log = LogFactory.getLog(DetachedEntityState.class);
private GraphDatabaseContext graphDatabaseContext;
private Neo4JPersistentEntity<ENTITY> persistentEntity;
private final GraphDatabaseContext graphDatabaseContext;
private final Neo4JPersistentEntity<ENTITY> persistentEntity;

public DetachedEntityState(final EntityState<ENTITY, STATE> delegate, GraphDatabaseContext graphDatabaseContext) {
this.delegate = delegate;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
Expand Down
Expand Up @@ -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<NodeBacked> {

private GraphDatabaseContext graphDatabaseContext;
Expand Down
Expand Up @@ -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() {
Expand Down