简体   繁体   English

OrientDB在Java代码中的边缘上创建约束

[英]OrientDB create constraints on edges in java code

I have a few nodes in orientDB and i want to have constraints on the edges between them. 我在orientDB中有几个节点,我想在它们之间的边缘上施加约束。 For example i have a node type Master and a type Slave and i want to have edges only between master and slave and not between slaves. 例如,我有一个主节点类型和一个从节点类型的节点,并且我希望仅在主节点和从节点之间而不是从节点之间具有边。

I want to do this using a constraint on the relationshiop between master and slave. 我想通过对主从之间的关系约束进行限制。 Is this possible using constraints? 使用约束是否可能?

Edit: I tried to do it like this, but it doesnt work, i can still add a slave pointing to a master. 编辑:我试图这样做,但它不起作用,我仍然可以添加一个指向主服务器的从属服务器。 How do i access the schema when using a graph DB? 使用图形数据库时如何访问架构?

OrientGraph graph = new OrientGraph("remote:localhost/test");
    if (graph.getEdgeType("Ref") == null) {
        graph.createEdgeType("Ref");
    }
    if (graph.getVertexType("Element") == null) {
        graph.createVertexType("Element");
    }
    if (graph.getVertexType("SubElement") == null) {
        OrientVertexType subElementVertex = graph
                .createVertexType("SubElement");
        OrientVertexType elementVertex = graph.getVertexType("Element");
        subElementVertex
                .createProperty("parent", OType.LINK, elementVertex)
                .isMandatory();
    }

I'm using this to create my vertices and edges: 我正在使用它来创建我的顶点和边缘:

Vertex page3 = graph.addVertex("class:Element");
Vertex doc1 = graph.addVertex("class:SubElement");
page3.setProperty("type", "page");
doc1.setProperty("type", "document");

Edge edge6 = graph.addEdge("class:Ref", page3, doc1, "Ref");
edge6.setProperty("properties", mapEdge);
edge6.setProperty("type", "document");

Edge edge7 = graph.addEdge("class:Ref", doc1, page3, "Ref");

And that last one should not work, if the constraint is correct. 如果约束是正确的,那么最后一个应该不起作用。

You can do this with the schema : 您可以使用以下模式执行此操作:

Master master = database.getMetadata().getSchema().createClass("Master");

Slave slave = database.getMetadata().getSchema().createClass("Slave");
slave.createProperty("parent", OType.LINK, master);

With graphs: 带有图形:

OrientEdgeType edge = graph.createEdgeType("MyEdge");
Edge e = subElementVertex.addEdge("Ref", elementVertex );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM