30 January 2014
30 January 2014,

When you work with Web Applications, it’s very common to query elements and render them to the user to let him to apply some changes. Once the user updates some fields and press the “save” button, what happens?

Before now the developer had to track the changes in a separate structure, load the vertex/edge from the database and apply the changes to the element.

Starting from OrientDB v1.7 we added 2 new methods to the Graph API against OrientElement and OrientBaseGraph classes:

  • - OrientElement.detach()
  • - OrientElement.attach()
  • - OrientBaseGraph.detach( OrientElement )
  • - OrientBaseGraph.attach( OrientElement )


Detach methods fetch all the record content in RAM and reset the connection to the Graph instance. This allow to modify the element off-line and re-attach it once finished.


Once the detached element has been modified, to be saved back to the database you need to call the attach() method. It restore back the connection between the Graph Element and the Graph Instance.


The first step is load some vertex and detach them.


OrientGraph g = OrientGraph(“plocal:/temp/db”);
  Iterable<OrientVertex> results = g.query().has(“name”, EQUALS, ‘fast’);
  for( OrientVertex v : results )
} finally {

After a while the element is updated (from GUI or by application)


v.setProperty(“name”, “super fast!”);

On “save” button re-attach the element and save it to the database.


OrientGraph g = OrientGraph(“plocal:/temp/db”);
  v.attach( g ); v.save();
} finally {


Does detach go recursively to detach all connected elements?

No, it works only at the current element level.

Can I add edge against detached elements?

No, you can only get/set/remove property while is detached. Any other operation that requires the database will throw an IllegalStateException.


Leave a Reply