0
votes

Let's say I use objectify to load() an entity from datastore, then "modify" properties, then save() it back to datastore.

Let's assume no property has changed, e.g. a property has been "overwritten" with it's previous value.

Would objectify still execute the save() or is smart enough to realize there were no real changes and omit saving.

In other words: Does objectify assume there is a change only because I write a property, or will it actually look at the property to determine if there was a material change?

1
The way i understand objectify it doesn't care about anything that you do to your entity object. It simply forwards the entities data to the datastore API making your question actually whether the datastore API performs a write (even if there is no change). Therefor I believe a write will occur. But my understanding of objectify has been wrong before, so i won't post an answer. Instead we might get a response from stickfigure on this question. It would be a very easy test to prove my theory though. Since you can see the number of writes in the dev server; simply save an unchanged entity twice. - konqi

1 Answers

2
votes

In short, your question is: Does Objectify provide dirty change detection?

No, it doesn't. At least not currently. This is mildly annoying sometimes, especially when you want to call several methods that may change an object - you don't want to execute multiple saves.

I've taken to writing code like this in my entities:

public void setFoo(final String value) {
    if (!Objects.equals(this.foo, value)) {
        this.foo = value;
        ofy().defer().save().entity(this);
    }
}

Which is a sort of poor-man's dirty change detection.