Update a Document


In a previous post I show you how to create a document. Now we will see how to update it assumming that we know both identifier and revision of the last version of the document persisted into the DataBase.

// Create a first document 
BaseDocument doc1 = new BaseDocument(); 
doc1.setProperty("name", "onabai"); 
db.createDocument(doc1); 
// Display Id and Revision for stored document 
System.out.printf("Id: %s Revision: %s\n", doc1.getId(), doc1.getRevision());

and get the following output:

Id: a2011483f0a0dce0eed2df14e600ac9a Revision: 1-fff06fa1e6224105f49dbecd6d8b7398

Then create a new Document object in memory, set identifier and revision and finally persist it invoking createOrUpdateDocument method on Database class.

// Update document (creating a new one and copying id and revision) 
BaseDocument doc2 = new BaseDocument(); 
doc2.setId(doc1.getId()); 
doc2.setRevision(doc1.getRevision()); 
doc2.setProperty("name", "OnaBai"); 
doc2.setProperty("salutation", "hello, OnaBai!"); 
db.createOrUpdateDocument(doc2); 

// Display Id and Revision for stored document 
System.out.printf("Id: %s Revision: %s\n", doc2.getId(), doc2.getRevision()); 
System.out.printf("Property name: %s\n", doc2.getProperty("name"));

the result is:

Id: a2011483f0a0dce0eed2df14e600ac9a Revision: 2-d3ce602af02ccbec2b0d9189469901e8 
Property name: OnaBai

The identifier is the same while revision has been updated.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s