Sorry for very dumb question. I'm using Morphia 1.00. Have some entity:
@Entity("Vacancy")
public class Vacancy {
@Id
private ObjectId id;
@Version
long version;
private String title;
and some other fields, setter and getters. Trying to save identical instances:
Vacancy vacancy1 = new Vacancy();
vacancy1.setTitle("Dumm");
Vacancy vacancy2 = new Vacancy();
vacancy2.setTitle("Dumm");
vacancyDao.getDatastore().save(vacancy1);
vacancyDao.getDatastore().save(vacancy2);
As I know, mongoDb must execute upsert command(means "update if present; insert (a single document) if missing"). But instead of just updating the _id field, mongo saves new entity in the DB.
When you dont specify the ObjectId for an object(document) which you are going to insert, morphia generates a 'new' ObjectId and sets it before the object(document) could inserted.
So, every object which doesn't have the ObjectId will be given new and unique objectId and the object will be treated as new "document".
If you want to update, do something like
Vacancy vacancy1 = new Vacancy();
vacancy1.setTitle("Dumm");
Vacancy vacancy2 = new Vacancy();
vacancy2.setTitle("Dummmmm");
vacancyDao.getDatastore().save(vacancy1);
vacancy2.setId(vacancy1.getId());
vacancyDao.getDatastore().save(vacancy2);//This line will update the "Dumm" to "Dummmmm"