Hi,
I have problem with persisting more relationships (same types) between two nodes. Each relationship has different properties. I know that I need to persist relationship directly because saving entity removes the duplicates. But saving relationship entity in some cases removes duplicates too. Look at my unit test. Whole test project is in attachment.
I think that there is no need to differentiate saving behavior between entity and relationship, because when i save duplicate relationship and later want to edit entity property value, my duplicate relationships will be removed.
I want to ask, how to correctly save more relationships with same type between same nodes and later not remove duplicate relationships by editing and saving entity node?
Code:import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.collection.IsCollectionWithSize.hasSize; import static org.junit.Assert.assertThat; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.neo4j.support.Neo4jTemplate; import org.springframework.data.neo4j.support.node.Neo4jHelper; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.BeforeTransaction; import org.springframework.transaction.annotation.Transactional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration public class MoreRelationshipsBetweenNodesTest { @Autowired private Neo4jTemplate template; @BeforeTransaction public void setUp() { Neo4jHelper.cleanDb(template); } @Test @Transactional public void failedTest(){ FirstEntity firstEntity = new FirstEntity(); firstEntity.setValue("A"); firstEntity = template.save(firstEntity); SecondEntity second1 = new SecondEntity(); second1.setValue("1"); second1 = template.save(second1); SecondEntity second2 = new SecondEntity(); second2.setValue("2"); second2 = template.save(second2); EntityRelationship rel; rel = firstEntity.addSecondEntity(second1, 1); template.save(rel); rel = firstEntity.addSecondEntity(second1, 2); template.save(rel); rel = firstEntity.addSecondEntity(second2, 3); template.save(rel); FirstEntity persistedEntity = template.findOne(firstEntity.getId(), FirstEntity.class); assertThat(persistedEntity.getId(), notNullValue()); assertThat(persistedEntity.getValue(), equalTo(firstEntity.getValue())); assertThat(persistedEntity.getEntityRelationships(), hasSize(3)); } @Test @Transactional public void okTest(){ FirstEntity firstEntity = new FirstEntity(); firstEntity.setValue("A"); firstEntity = template.save(firstEntity); SecondEntity second1 = new SecondEntity(); second1.setValue("1"); second1 = template.save(second1); SecondEntity second2 = new SecondEntity(); second2.setValue("2"); second2 = template.save(second2); EntityRelationship rel; rel = firstEntity.addSecondEntity(second2, 3); template.save(rel); rel = firstEntity.addSecondEntity(second1, 1); template.save(rel); rel = firstEntity.addSecondEntity(second1, 2); template.save(rel); FirstEntity persistedEntity = template.findOne(firstEntity.getId(), FirstEntity.class); assertThat(persistedEntity.getId(), notNullValue()); assertThat(persistedEntity.getValue(), equalTo(firstEntity.getValue())); assertThat(persistedEntity.getEntityRelationships(), hasSize(3)); } }


Reply With Quote