在 OrientDB 中将顶点添加到框架图中

Add vertex to framed graph in OrientDB

我不确定这是 OrientDB 的问题还是操作错误。在下面的代码中,我希望创建 Person 的新实例,但是,新顶点不会作为 Person 对象进入,而是最终出现在 "V" 集合中。

这已得到部分解决 here,但是这是在我使用 OrientGraph 的地方使用 TinkerGraph。根据链接页面中的评论,我相信我所做的应该有效。有什么我想念的吗?

package simpleFrames;

import com.tinkerpop.frames.Property;
import com.tinkerpop.frames.VertexFrame;

public interface Person extends VertexFrame {

    @Property("firstName")
    public void setFirstName(String name);

    @Property("firstName")
    public String getFirstName();

}

.

package simpleFrames;

import java.io.IOException;

import com.orientechnologies.orient.client.remote.OServerAdmin;
import com.orientechnologies.orient.core.metadata.schema.OType;
import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.orient.OrientGraph;
import com.tinkerpop.blueprints.impls.orient.OrientGraphFactory;
import com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx;
import com.tinkerpop.blueprints.impls.orient.OrientVertexType;
import com.tinkerpop.frames.FramedGraph;
import com.tinkerpop.frames.FramedGraphFactory;
import com.tinkerpop.frames.modules.javahandler.JavaHandlerModule;

public class go {

    static String remote = "localhost";
    static String database = "muck";
    static String username = "admin";
    static String password = "admin";
    static String rootPassword = "root";

    public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException {

        String location = String.format("remote:%s", remote);
        String fullDatabaseName = String.format("%s/%s", location, database);

        OServerAdmin admin = new OServerAdmin(location);
        try {
            admin.connect("root", rootPassword);
            admin.createDatabase(fullDatabaseName, "graph", "memory");

        } finally {
            admin.close();
        }

        OrientGraphFactory factory = new OrientGraphFactory(fullDatabaseName, username, password);
        try {
            OrientGraphNoTx noTx = factory.getNoTx();
            try {
                OrientVertexType person = noTx.createVertexType("Person");
                person.createProperty("firstName", OType.STRING);

                noTx.commit();

            } finally {
                noTx.shutdown();
            }

            OrientGraph graph = factory.getTx();
            try {
                FramedGraph<OrientGraph> framedGraph = new FramedGraphFactory(new JavaHandlerModule()).create(graph);

                Person mark = framedGraph.addVertex(null, Person.class);
                mark.setFirstName("Mark");

                Person frank = framedGraph.addVertex(null, Person.class);
                frank.setFirstName("Frank");

                graph.commit();

                /* Records are going in as V */
                for (Vertex v : graph.getVerticesOfClass("V")) {
                    Person person = framedGraph.frame(v, Person.class);
                    System.out.println(String.format("Vertex: %s", person.getFirstName()));
                }

                /* They are NOT going in as Person */
                for (Vertex v : graph.getVerticesOfClass("Person")) {
                    Person person = framedGraph.frame(v, Person.class);
                    System.out.println(String.format("Person: %s", person.getFirstName()));
                }
            } finally {
                graph.shutdown();
            }

        } finally {
            factory.close();
            OServerAdmin admin2 = new OServerAdmin(fullDatabaseName);
            try {
                admin2.connect("root", rootPassword);
                admin2.dropDatabase(database);
            } finally {
                admin2.close();
            }
        }
    }
}

好吧,我已经找到了解决我的问题的方法,我将它发布在这里供下一个人使用。

调用时...

FramedGraph<OrientGraph>.addVertex(Object id, Class<F> kind)

...第一个参数用于多种用途(感谢任何能在文档中找到它的人!)。这里的 'kind' 参数实际上对数据库中的哪个 class 用于存储数据没有影响,而是调用应该看起来像

framedGraph.addVertex("class:Person", Person.class);