From 769e89251ac0554e800dfd9e86488e1efe8454fb Mon Sep 17 00:00:00 2001 From: Michael Hunger Date: Fri, 13 Aug 2010 01:42:09 +0200 Subject: [PATCH] added managedset for operations on 1:n --- .../graph/neo4j/Neo4jNodeBacking.aj | 41 +++++++++++++++++-- .../persistence/test/Person.java | 5 ++- .../test/graph/Neo4jGraphPersistenceTest.java | 14 +++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj index 1a6ea1f93..b0573853b 100644 --- a/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj +++ b/src/main/java/org/springframework/persistence/graph/neo4j/Neo4jNodeBacking.aj @@ -1,8 +1,10 @@ package org.springframework.persistence.graph.neo4j; import java.lang.reflect.Field; +import java.util.AbstractSet; import java.util.Collection; import java.util.HashSet; +import java.util.Iterator; import java.util.Set; import org.aspectj.lang.ProceedingJoinPoint; @@ -142,7 +144,7 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields Neo4J simple node property [" + propName + "] with value=[" + newVal + "]"); - return null; + return proceed(entity, newVal); } RelationshipInfo relInfo = relationshipInfoFactory.forField(f); @@ -267,6 +269,37 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields { + private final NodeBacked entity; + final Set delegate; + private final RelationshipInfo relationshipInfo; + + private ManagedSet(NodeBacked entity, Object newVal, RelationshipInfo relationshipInfo) { + this.entity = entity; + this.relationshipInfo = relationshipInfo; + delegate = (Set) newVal; + } + + @Override + public Iterator iterator() { + return delegate.iterator(); + } + + @Override + public int size() { + return delegate.size(); + } + + @Override + public boolean add(NodeBacked e) { + boolean res=delegate.add(e); + if (res) { + relationshipInfo.apply(entity, delegate); + } + return res; + } + } + private final RelationshipType type; private final Direction direction; private final Class relatedType; @@ -279,7 +312,7 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields newNodes=new HashSet(); @@ -314,7 +347,7 @@ public aspect Neo4jNodeBacking extends AbstractTypeAnnotatingMixinFields()); + group.addPerson(michael); + group.addPerson(david); + Collection personsFromGet = group.getPersons(); + Assert.assertEquals(new HashSet(Arrays.asList(david,michael)), personsFromGet); + Assert.assertTrue(Set.class.isAssignableFrom(personsFromGet.getClass())); + } + @Test @Transactional public void testInstantiatedFinder() {