使用UniqueNodeFactory创建节点时的ConstraintViolationException

时间:2016-03-02 19:32:32

标签: neo4j

此测试失败但错误,我不明白为什么.. 我认为UniqueNodeFactory只有在它不存在时才创建节点。 当然我可以使用Cypher做同样的事情,但我想了解这里发生的事情.. 有人可以解释一下吗? 我正在使用neo4j 2.3.1。

public class SimpleTest {

private GraphDatabaseService graphService;

@Before
public void setUp() throws Exception {
    graphService = new TestGraphDatabaseFactory().newImpermanentDatabase();
    graphService.execute("CREATE CONSTRAINT ON (user:User) ASSERT user.userId IS UNIQUE");
}

@After
public void tearDown() throws Exception {
    graphService.shutdown();
}

public static UniqueFactory.UniqueNodeFactory createUserFactory(GraphDatabaseService graphDatabaseService) {

    return new UniqueFactory.UniqueNodeFactory(graphDatabaseService, "User") {

        @Override
        protected void initialize(Node created, Map<String, Object> properties) {
            created.addLabel(DynamicLabel.label("User"));
            created.setProperty("userId", properties.get("userId"));
        }
    };
}

@Test
public void testCreateUser() throws Exception {
    try (Transaction tx = graphService.beginTx()) {
        Node node = graphService.createNode(DynamicLabel.label("User"));
        node.setProperty("userId", 100L);
        tx.success();
    }

    try (Transaction tx = graphService.beginTx()) {
        UniqueFactory.UniqueNodeFactory uniqueFactory = createUserFactory(graphService);
        uniqueFactory.getOrCreate("userId", 100L);
        tx.success();
    }
}

}

错误:

Caused by: org.neo4j.kernel.api.exceptions.schema.UniquePropertyConstraintViolationKernelException: Node 0 already exists with label 0 and property 0=100
at org.neo4j.kernel.impl.api.ConstraintEnforcingEntityOperations.validateNoExistingNodeWithLabelAndProperty(ConstraintEnforcingEntityOperations.java:165)
at org.neo4j.kernel.impl.api.ConstraintEnforcingEntityOperations.nodeSetProperty(ConstraintEnforcingEntityOperations.java:140)
at org.neo4j.kernel.impl.api.LockingStatementOperations.nodeSetProperty(LockingStatementOperations.java:453)
at org.neo4j.kernel.impl.api.OperationsFacade.nodeSetProperty(OperationsFacade.java:896)
at org.neo4j.kernel.impl.core.NodeProxy.setProperty(NodeProxy.java:293)
... 33 more

1 个答案:

答案 0 :(得分:1)

好的,现在我明白了原因。我使用Cypher创建了架构索引,dfout <- 'sam1 sam2 sam3 7.600902 8.006368 8.294050 7.600902 7.313220 7.090077 7.600902 8.853665 8.699515' dfout <- read.table(text=dfout, header=T) 使用传统索引。要解决此问题,您只需使用UniqueNodeFactory创建节点。

相关问题