Search code examples
javatitangremlintinkerpop3

How to add property to vertex property in java?


I want to add property to a vertex property. In gremlin i add property "phone" to vertex property "places" that has value is "place1"

g.V(v).properties('places').hasValue('place1').property('phone',"123456789")

It worked ok without using transaction commit. But when i used this way in java code, it not worked. So in java code, how to add a property to vertex property? Thank for your help.


Solution

  • You need to iterate() the traversal.

    g.V(v).properties('places').hasValue('place1').property('phone',"123456789").iterate()
    

    One way to think about: the original code snippet is the query, but then you still need to execute it.

    Here's a full Gremlin Console example that shows the difference.

    gremlin> graph = TitanFactory.open('inmemory'); g = graph.traversal()
    ==>graphtraversalsource[standardtitangraph[inmemory:[127.0.0.1]], standard]
    gremlin> v = graph.addVertex('name','jenny','places','home')
    ==>v[4264]
    gremlin> g.V(v).properties('places').hasValue('home')
    ==>vp[places->home]
    gremlin> g.V(v).properties('places').hasValue('home').property('phone','867-5309'); 'traversal was not iterated'
    ==>traversal was not iterated
    gremlin> g.V(v).properties('places').hasValue('home').properties()
    gremlin> g.V(v).properties('places').hasValue('home').property('phone','867-5309').iterate(); 'iterated!'
    ==>iterated!
    gremlin> g.V(v).properties('places').hasValue('home').properties()
    ==>p[phone->867-5309]
    gremlin> graph.tx().commit()
    ==>null
    

    You need to commit the transaction if you want the data to be persisted.