简体   繁体   English

我可以将我的Java类注册为OrientDB Vertices和/或Edges吗?

[英]Can I register my Java classes as OrientDB Vertices and/or Edges?

I tried the OrientDB Object Database API , which allows to use register Java POJOs to the database with entityManager.registerEntityClasses(packagename) and then read write them with minimal extra effort. 我尝试了OrientDB 对象数据库API ,它允许使用entityManager.registerEntityClasses(packagename)将注册Java POJO用于​​数据库,然后用最少的额外工作读取它们。

However, what I would like to achieve is to register my Java POJOs as Vertices in a graph. 但是,我想要实现的是在图表中将我的Java POJO注册为顶点。 Is there some mapping available for registering Java POJOs as Vertices? 是否有一些映射可用于将Java POJO注册为顶点?

Sorry, GraphAPI can't be bound to Object Database API. 抱歉,GraphAPI无法绑定到Object Database API。 You could use TinkerPop Frames for this purpose. 您可以使用TinkerPop框架来实现此目的。

Since TinkerPop Frame was very restricting for me I saved a lot of boilerplate code by making my Vertex to Object mapper whenever retrieving a Vertex from the Database. 由于TinkerPop Frame对我来说非常有限,因此每当从数据库中检索Vertex时,我都会通过制作Vertex to Object映射器来节省大量的样板代码。 That way it almost felt that I was using objects all the time without setting individual properties etc..even though I was mainly using graph api. 这样,我几乎感觉到我一直在使用对象而没有设置个别属性等等。即使我主要使用图形api。

This code is a basic example and it doesn't cover all the cases but only when you have a clean bean with setters on those variables that are either primitives or Strings. 这段代码是一个基本的例子,并没有涵盖所有的情况,只有当你有一个干净的bean上有这些变量的基础或字符串的setter时。 Also keep in mind that this is less performant than direct invocation since it uses reflection. 还要记住,这比直接调用要低,因为它使用反射。 Hope this will be useful to someone. 希望这对某人有用。 I am also using a snippet from here ( https://stackoverflow.com/a/1042827/986160 ) to get all the inherited fields. 我也在这里使用一个片段( https://stackoverflow.com/a/1042827/986160 )来获取所有继承的字段。

public class ObjectMapper<T> {  

    public static List<Field> getAllFields(List<Field> fields, Class<?> type)       {
         fields.addAll(Arrays.asList(type.getDeclaredFields()));

         if (type.getSuperclass() != null) {
             fields = getAllFields(fields, type.getSuperclass());
         }

         return fields;
    }

    public Vertex mapObjToVertex(T obj, Vertex v){
        if (obj == null ) { return null; }

        for (Field field : getAllFields(new LinkedList<Field>(), obj.getClass())) {
            field.setAccessible(true);

            String name = field.getName();
            String type = field.getType().getSimpleName();
            boolean isPrimitive = field.getType().isPrimitive();
            if(isPrimitive || type.equals("String")){
                try {
                    Object value = field.get(obj);
                    if(value!=null){
                        v.setProperty(name,value);
                    }
                } catch (IllegalArgumentException | IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return v;
    }


    public T mapVertexToObj(Vertex v, T obj) {
        if (v == null) { return null; }

        for (Field field : getAllFields(new LinkedList<Field>(), obj.getClass())) {
            field.setAccessible(true);

            String name = field.getName();
            String type = field.getType().getSimpleName();
            boolean isPrimitive = field.getType().isPrimitive();


            if(!isPrimitive && !type.equals("String")) continue;

            Method setter;
            try {
                setter = new PropertyDescriptor(name, obj.getClass()).getWriteMethod();

            }
            catch(IntrospectionException ie) { continue; }

            try {
                if(name.equals("id")){
                    setter.invoke(obj, v.getId().toString());
                }
                else {
                    Object storedValue = v.getProperty(name);
                    if (storedValue !=null){
                        if(type.equals("String")) setter.invoke(obj, storedValue.toString());
                        else if(type.equals("byte")) setter.invoke(obj, (byte)storedValue);
                        else if(type.equals("int")) setter.invoke(obj, (int)storedValue);
                        else if(type.equals("float")) setter.invoke(obj, (float)storedValue);
                        else if(type.equals("long")) setter.invoke(obj, (long)storedValue);
                        else if(type.equals("double")) setter.invoke(obj, (double)storedValue);
                        else if(type.equals("boolean")) setter.invoke(obj,(boolean)storedValue);
                    }
                }

            }
            catch(Exception e){
                e.printStackTrace();
            }

        }
        return obj;
    }
}

And it is used like that: 它是这样使用的:

Entity entity = new ObjectMapper<Entity>().mapVertexToObj(v,new Entity());

or 要么

Vertex v = new ObjectMapper<Entity>().mapObjToVertex(entity,v);

After you registered your classes, you can do: 注册课程后,您可以:

OSchema schema = db.getMetadata().getSchema();
schema.getClass("SomeVertexClass").setSuperClass(schema.getClass("V"));
schema.getClass("SomeEdgeClass").setSuperClass(schema.getClass("E"));

however, to remove a vertex or edge with an OObjectDatabaseTx connection you cannot simply call: 但是,要使用OObjectDatabaseTx连接删除顶点或边,您不能简单地调用:

db.delete(somePojo);

Instead, you need to do something like 相反,你需要做类似的事情

db.command(new OSQLSynchQuery("delete vertex/edge " + somePojo.getId())).execute();

For a library which does this and cascade relations for you, see: ObjectGraphDb 对于为您执行此操作和级联关系的库,请参阅: ObjectGraphDb

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

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