google-code-export / morphia

Automatically exported from code.google.com/p/morphia
1 stars 0 forks source link

Version control with merge() #262

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Datastore method merge() currently doesn't support @Version control like save() 
does (or it doesn't work for me)

Sometimes you have to use database which is used by other systems (and have to 
use merge() to preserve fields not defined in you entities). If you also want 
to use optimistic locking you can't do it now.

It would be nice to have method like:
public <T> Key<T> merge(T entity, boolean checkVersion)

Which would allow something like this (using 
http://code.google.com/p/morphia/wiki/MongoNewsletterArticleDec2010 as base):

Entity:
class Person {
  @Id String name;
  String phone;

  @Version
  long version;
}

//Setup:
Datastore ds = ...;

Person me = new Person("Damian");

ds.save(me) // all good

//Version control:
Person meCopy1 = ds.get(Person.class, "Damian");
Person meCopy2 = ds.get(Person.class, "Damian");

meCopy1.setPhone("111-376-7379"); 
ds.merge(meCopy1, true);

try {
  meCopy2.setPhone("123-376-7379");
  ds.merge(meCopy2, true);
} catch (ConcurrentModificationException e) {
  // ooppsss... 
}

Original issue reported on code.google.com by MomotDam...@gmail.com on 5 Apr 2011 at 8:59

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r1743.

Original comment by scotthernandez on 5 Apr 2011 at 9:43