DATAGRAPH-321 - Fixes for TypeRepresentation-Strategies

This commit is contained in:
Michael Hunger
2012-12-19 10:52:34 +01:00
parent 5ad3d3a118
commit 8f88945582
6 changed files with 66 additions and 3 deletions

View File

@@ -217,6 +217,16 @@ public class SubReferenceNodeTypeRepresentationStrategyTest extends EntityTestBa
assertEquals(subThing, newThing);
}
@Test
public void testSaveTwice() throws Exception {
Thing thing = new Thing();
thing.setName("Foo");
thing = neo4jTemplate.save(thing);
thing.setName("Bar");
thing = neo4jTemplate.save(thing);
neo4jTemplate.findOne(thing.getNodeId(),Thing.class);
}
@Test
@Transactional
public void testProjectEntity() throws Exception {

View File

@@ -79,6 +79,7 @@ public class MappingInfrastructureFactoryBean implements FactoryBean<Infrastruct
private MappingInfrastructure mappingInfrastructure;
private TypeRepresentationStrategyFactory.Strategy typeRepresentationStrategy;
public MappingInfrastructureFactoryBean(GraphDatabase graphDatabase, PlatformTransactionManager transactionManager) {
this.graphDatabase = graphDatabase;
@@ -118,7 +119,7 @@ public class MappingInfrastructureFactoryBean implements FactoryBean<Infrastruct
relationshipEntityInstantiator = new RelationshipEntityInstantiator(entityStateHandler);
}
if (this.typeRepresentationStrategyFactory == null) {
this.typeRepresentationStrategyFactory = new TypeRepresentationStrategyFactory(graphDatabase);
this.typeRepresentationStrategyFactory = typeRepresentationStrategy!=null ? new TypeRepresentationStrategyFactory(graphDatabase,typeRepresentationStrategy) : new TypeRepresentationStrategyFactory(graphDatabase);
}
if (this.nodeTypeRepresentationStrategy == null) {
this.nodeTypeRepresentationStrategy = typeRepresentationStrategyFactory.getNodeTypeRepresentationStrategy();
@@ -271,6 +272,9 @@ public class MappingInfrastructureFactoryBean implements FactoryBean<Infrastruct
public void setTypeRepresentationStrategyFactory(TypeRepresentationStrategyFactory typeRepresentationStrategyFactory) {
this.typeRepresentationStrategyFactory = typeRepresentationStrategyFactory;
}
public void setTypeRepresentationStrategy(TypeRepresentationStrategyFactory.Strategy strategy) {
this.typeRepresentationStrategy = strategy;
}
public void setIndexProvider(IndexProvider indexProvider) {
this.indexProvider = indexProvider;

View File

@@ -55,6 +55,7 @@ public abstract class AbstractIndexingTypeRepresentationStrategy<S extends Prope
@Override
public void writeTypeTo(S state, StoredEntityType type) {
if (type.getAlias().equals(state.getProperty(TYPE_PROPERTY_NAME, null))) return; // already there
addToTypesIndex(state, type);
state.setProperty(TYPE_PROPERTY_NAME, type.getAlias());
}

View File

@@ -95,6 +95,10 @@ public class SubReferenceNodeTypeRepresentationStrategy implements NodeTypeRepre
@Override
public void writeTypeTo(Node state, StoredEntityType type) {
final Node subReference = obtainSubreferenceNode(type);
for ( Relationship relationship : state.getRelationships( INSTANCE_OF_RELATIONSHIP_TYPE, Direction.OUTGOING ) )
{
if (relationship.getEndNode().equals( subReference )) return; // already there
}
state.createRelationshipTo(subReference, INSTANCE_OF_RELATIONSHIP_TYPE);
subReference.setProperty(SUBREF_CLASS_KEY, type.getAlias());
if (log.isDebugEnabled()) log.debug("Created link to subref node: " + subReference + " with type: " + type.getType().getSimpleName()+" alias "+type.getAlias());

View File

@@ -16,6 +16,7 @@
package org.springframework.data.neo4j.support.typerepresentation;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.NotFoundException;
import org.neo4j.graphdb.PropertyContainer;
@@ -45,7 +46,7 @@ public class TypeRepresentationStrategyFactory {
this.graphDatabaseService = graphDatabaseService;
this.strategy = strategy;
}
public TypeRepresentationStrategyFactory(GraphDatabase graphDatabaseService,Strategy strategy, IndexProvider indexProvider) {
this.indexProvider = indexProvider;
this.graphDatabaseService = graphDatabaseService;
@@ -92,7 +93,7 @@ public class TypeRepresentationStrategyFactory {
this.indexProvider = indexProvider;
}
private enum Strategy {
public enum Strategy {
SubRef {
@Override
public NodeTypeRepresentationStrategy getNodeTypeRepresentationStrategy(GraphDatabase graphDatabaseService, IndexProvider indexProvider) {

View File

@@ -0,0 +1,43 @@
/**
* Copyright 2011 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.neo4j.typerepresentation;
import org.junit.Test;
import org.neo4j.graphdb.Transaction;
import org.neo4j.test.ImpermanentGraphDatabase;
import org.springframework.data.neo4j.model.Person;
import org.springframework.data.neo4j.support.MappingInfrastructure;
import org.springframework.data.neo4j.support.MappingInfrastructureFactoryBean;
import org.springframework.data.neo4j.support.Neo4jTemplate;
import org.springframework.data.neo4j.support.typerepresentation.SubReferenceNodeTypeRepresentationStrategy;
import org.springframework.data.neo4j.support.typerepresentation.TypeRepresentationStrategyFactory;
public class TypeRepresentationTest {
@Test
public void testSavingTwiceResultsOnlyInOneTRSCall() throws Exception {
ImpermanentGraphDatabase db = new ImpermanentGraphDatabase();
MappingInfrastructureFactoryBean factoryBean = new MappingInfrastructureFactoryBean(db, null);
factoryBean.setTypeRepresentationStrategy(TypeRepresentationStrategyFactory.Strategy.SubRef);
factoryBean.afterPropertiesSet();
Neo4jTemplate template = new Neo4jTemplate(factoryBean.getObject());
Transaction tx = db.beginTx();
Person person = template.save(new Person());
person.setName("Bar");
template.save(person);
tx.failure();
tx.finish();
}
}