I have been trying to use the enforceTargetType parameter to discriminate relationships based on the end node type but am getting an llegalStateException.
I have used the example provided by spring-data-neo4j verbatim: (http://docs.spring.io/spring-data/neo4j/docs/current/reference/html/#reference_programming_model_relationships_relationshiptypediscrimination)
My Node Entity classes are:
@NodeEntity
class Person {
@GraphId
private Long id;
@RelatedTo(type="OWNS", enforceTargetType=true)
Car car;
@RelatedTo(type="OWNS", enforceTargetType=true)
Pet pet;
}
@NodeEntity
public class Car {
@GraphId
private Long id;
@RelatedTo(type="OWNS", enforceTargetType=true, direction=Direction.INCOMING)
Person person;
}
@NodeEntity
public class Pet {
@GraphId
private Long id;
@RelatedTo(type="OWNS", enforceTargetType=true, direction=Direction.INCOMING)
Person person;
}
Test Class:
public class TestPerson {
@Autowired Neo4jTemplate neo4jTemplate;
@Test public void testPerson (){
Person pers = new Person();
Car car = new Car();
Pet pet = new Pet();
pers.car = car;
pers.pet = pet;
neo4jTemplate.save(pers);
}
}
I am getting the following exception:
java.lang.IllegalStateException: Could not read type 'Pet' - type does not exist
at org.springframework.data.neo4j.fieldaccess.RelationshipHelper.removeMissingRelationshipsInStoreAndKeepOnlyNewRelationShipsInSet(RelationshipHelper.java:85)
at org.springframework.data.neo4j.fieldaccess.RelatedToFieldAccessor.removeMissingRelationships(RelatedToFieldAccessor.java:73)
at org.springframework.data.neo4j.fieldaccess.RelatedToSingleFieldAccessorFactory$RelatedToSingleFieldAccessor.setValue(RelatedToSingleFieldAccessorFactory.java:67)
at org.springframework.data.neo4j.fieldaccess.DefaultEntityState.setValue(DefaultEntityState.java:113)
at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.setEntityStateValue(SourceStateTransmitter.java:70)
at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.access$100(SourceStateTransmitter.java:40)
at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter$4.doWithAssociation(SourceStateTransmitter.java:113)
at org.springframework.data.mapping.model.BasicPersistentEntity.doWithAssociations(BasicPersistentEntity.java:207)
at org.springframework.data.neo4j.support.mapping.SourceStateTransmitter.copyPropertiesTo(SourceStateTransmitter.java:109)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:150)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:179)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:243)
at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:231)
at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:293)
at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:287)
at com.poc.neo4j.person.TestPerson.testPerson(TestPerson.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.test.context.junit4.SpringTestMethod.invoke(SpringTestMethod.java:198)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod(SpringMethodRoadie.java:274)
at org.springframework.test.context.junit4.SpringMethodRoadie$2.run(SpringMethodRoadie.java:207)
at org.springframework.test.context.junit4.SpringMethodRoadie.runBeforesThenTestThenAfters(SpringMethodRoadie.java:254)
at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions(SpringMethodRoadie.java:234)
at org.springframework.test.context.junit4.SpringMethodRoadie.runTest(SpringMethodRoadie.java:204)
at org.springframework.test.context.junit4.SpringMethodRoadie.run(SpringMethodRoadie.java:146)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod(SpringJUnit4ClassRunner.java:151)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: Pet
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.springframework.data.neo4j.fieldaccess.RelationshipHelper.removeMissingRelationshipsInStoreAndKeepOnlyNewRelationShipsInSet(RelationshipHelper.java:81)
... 38 more
This problem disappears if I give the Person->Pet and Person->Cat relatinships their own relationship type instead of using OWNS for both
Any help appreciated ...