为什么简单设置然后在动态代理上获取不持久? (使用TinkerPop Frames JavaHandler) [英] Why simple set and then get on Dynamic Proxy does not persist? (using TinkerPop Frames JavaHandler)

查看:103
本文介绍了为什么简单设置然后在动态代理上获取不持久? (使用TinkerPop Frames JavaHandler)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在实现VertexFrame的类上添加简单的getter和setter,我为此使用了JavaHandlers.对于那些方法,我不想与数据库进行任何交互.不幸的是,没有@Ignore之类的东西,所以我没有意外的注释异常.当我在代理服务器上设置某些内容并在经过反射后立即执行获取操作时,将不会存储任何内容.可能是我不应该使用JavaHandlers,而是使用其他东西.Btw manager.frame返回Java动态代理对象(java.lang.reflect.Proxy).这是失败的测试:

I wanted to add simple getters and setters on a class that implements VertexFrame and I used JavaHandlers for those. For those methods I didn't want to have any interaction with the database. Unfortunately there is not something like @Ignore so I don't have unexpected annotation exceptions. When I set something on the proxy and immediately I do a get after it goes through reflection, nothing is stored. Could be that I shouldn't be using JavaHandlers but something else.. Btw manager.frame returns Java Dynamic Proxy objects (java.lang.reflect.Proxy). Here is the failing test:

package com.tests.testbed;
import org.springframework.util.Assert;

import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import com.tinkerpop.blueprints.impls.tg.TinkerGraphFactory;
import com.tinkerpop.frames.FramedGraph;
import com.tinkerpop.frames.FramedGraphFactory;
import com.tinkerpop.frames.modules.javahandler.JavaHandlerModule;

public class testProxy {
    public static void main(String args[]){
        TinkerGraph graph = TinkerGraphFactory.createTinkerGraph();
        FramedGraphFactory framedFactory = new FramedGraphFactory(new JavaHandlerModule());
        FramedGraph<TinkerGraph> manager = framedFactory.create(graph);
        Vertex vertex = manager.getVertex(1);
        IVert vert = manager.frame(vertex, IVert.class);
        int testVal = 231;
        vert.setTestVar(231);
        Assert.state(vert.getTestVar() != testVal, "int was not stored!");
    }

}

---------------------

package com.tests.testbed;

import com.tinkerpop.frames.Property;
import com.tinkerpop.frames.VertexFrame;
import com.tinkerpop.frames.modules.javahandler.JavaHandler;
import com.tinkerpop.frames.modules.javahandler.JavaHandlerClass;

@JavaHandlerClass(Vert.class)
public interface IVert extends VertexFrame {
    @Property("id")
    public int getId();
    @Property("id")
    public void setId(int id);

    @JavaHandler
    public void setTestVar(int testVar);

    @JavaHandler
    public int getTestVar();
}

--------------------

package com.tests.testbed;

import com.tinkerpop.blueprints.Vertex;
import com.tinkerpop.blueprints.impls.tg.TinkerGraph;
import com.tinkerpop.blueprints.impls.tg.TinkerGraphFactory;

public class Vert implements IVert {

    private Vertex vertex;
    private int id;
    private int testVar;

    public void setTestVar(int testVar){
        this.testVar = testVar;
    }

    public int getTestVar(){
        return this.testVar;
    }

    @Override
    public Vertex asVertex() {
        if (this.vertex == null){
            TinkerGraph graph = TinkerGraphFactory.createTinkerGraph();
            Vertex v = graph.getVertex(this.getId());
            this.vertex = v;
        }
        return this.vertex;
    }

    @Override
    public int getId() {
        return this.id;
    }

    @Override
    public void setId(int id) {
        this.id = id;

    }
}

非常感谢. PS,我已经将此添加为一个问题,以防出现错误: https://github.com/tinkerpop/frames/issues/109 我尝试获取TargetObject,但未能成功.请让我知道是否有任何解决方案可以添加可保留在代理服务器上的非数据库数据.

Thank you very much. P.S I have already added this as an issue in case it is a bug: https://github.com/tinkerpop/frames/issues/109 I tried getting the TargetObject but I couldn't. Please let me know if there is any solution for adding non-database data that can persist on Proxies.

推荐答案

首先在几个地方出了错:

You've gone wrong in a couple of places, first of all:

Property key is reserved for all elements: id

基本上,您不能在@Property("id")批注中使用属性值"id".

Basically you can't use the property value "id" in your @Property("id") annotations.

第二,尽管它没有失败,但是您的Vert类应该:

Secondly, although it doesn't fail, your Vert class should:

  • 实施JavaHandlerContext<Vertex>
  • 成为abstract
  • 使用Vertex的属性保持值(局部变量不会存储在图形数据库中!)
  • 实施/覆盖用@JavaHandler
  • 注释的方法
  • implement JavaHandlerContext<Vertex>
  • be abstract
  • persist values using the Vertex's properties (local variables are NOT stored in the graph db!)
  • only implement/override the methods annotated with @JavaHandler

此外,您不需要存储Vertex.由于IVert界面扩展了VertexFrame,因此您可以使用asVertex()方法访问Vertex.

Additionally, you don't need to store the Vertex. Because your IVert interface extends VertexFrame, you have access to the Vertex using the asVertex() method.

您绝对应该重新阅读文档,请参考示例- https://github.com/tinkerpop/frames/wiki/Java-Handler

You should definitely re-read the documentation, refer to the examples - https://github.com/tinkerpop/frames/wiki/Java-Handler

这里是重写/工作类. NB .我正在使用Groovy-Java应该完全相同/非常相似.

Here are the re-written/working classes. N.B. I was using Groovy - it should be exactly the same/very similar for Java.

@JavaHandlerClass(Vert.class)
public interface IVert extends VertexFrame {

    @Property("xxid")
    public int getId();

    @Property("xxid")
    public void setId(int id);

    @JavaHandler
    public void setTestVar(int testVar);

    @JavaHandler
    public int getTestVar();

}

证书

abstract class Vert implements JavaHandlerContext<Vertex>, IVert {

    public void setTestVar(int testVar){
        asVertex().setProperty('foobar', testVar);
    }

    public int getTestVar(){
        return (int)asVertex().getProperty('foobar');
    }

}

主要方法(Groovy)

def g = TinkerGraphFactory.createTinkerGraph()
FramedGraphFactory factory = new FramedGraphFactory(new JavaHandlerModule())
FramedGraph framedGraph = factory.create(g)
IVert vert = framedGraph.addVertex('myuniqueid', IVert)
vert.setId(123)
vert.setTestVar(456)
IVert vert2 = framedGraph.getVertex('myuniqueid', IVert)
assert vert2.id == 123
assert vert2.testVar == 456

这篇关于为什么简单设置然后在动态代理上获取不持久? (使用TinkerPop Frames JavaHandler)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆