30 January 2014
30 January 2014,
 0

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

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.

Attach

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.

Example

The first step is load some vertex and detach them.

 

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

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”);
try{
  v.attach( g ); v.save();
} finally {
  g.shutdown();
}

FAQ

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

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>