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;
}
}
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:
Basically you can't use the property value "id" in your
@Property("id")
annotations.Secondly, although it doesn't fail, your
Vert
class should:JavaHandlerContext<Vertex>
abstract
Vertex
's properties (local variables are NOT stored in the graph db!)@JavaHandler
Additionally, you don't need to store the
Vertex
. Because yourIVert
interface extendsVertexFrame
, you have access to theVertex
using theasVertex()
method.You should definitely re-read the documentation, refer to the examples - https://github.com/tinkerpop/frames/wiki/Java-Handler
Here are the re-written/working classes. N.B. I was using Groovy - it should be exactly the same/very similar for Java.
IVert
Vert
Main method (Groovy)