Entity Update - Is the documentation up to date?

#1

I'm working on my first ObjectDB project, and I just went back and reviewed some of the documentation.  Turns out I've been doing two things that http://www.objectdb.com/java/jpa/persistence/update suggests should not work.

First, I've been modifying managed entities from outside an active transaction.  If I subsequently begin and commit a transaction, all my changes seem to be merged automatically.

Second, I'm using an array of entities inside another entity, and setting the elements of that array without calling JDOHelper.makeDirty.  These changes are also automatically detected and saved.

Am I doing something wrong that may stop working without warning?

#2

The online manual is always expected to be up to date with the last ObjectDB version.

> First, I've been modifying managed entities from outside an active transaction.  If I subsequently begin and commit a transaction, all my changes seem to be merged automatically.

The more conventional way is to update entities within an active transaction, or update entities outside the transaction and then merge them within an active transaction. Your method should work as well but it may be less portable.

> I'm using an array of entities inside another entity, and setting the elements of that array without calling JDOHelper.makeDirty.  These changes are also automatically detected and saved.

These changes are detected if your classes are not enhanced. If your classes are enhanced, these changes will not trigger making the containing entity object dirty, but if it is marked dirty because of another change, the array update will also be applied to database on transaction commit.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#3

I'd like to understand the "dirtying" of entities a little better.  If an array reference (the array itself, not an element) is changed, will that be detected?  It's just changes to elements that are not detected in enhanced classes, correct?

If a class variable is assigned a new value that is the same as the old value, will the entity be marked dirty?  If I want to avoid unnecessary interactions with the DB, will I have to test the old value myself before assigning a (potentially identical) new value?

#4

The behavior is different for enhanced classes and non enhanced classes.

With non enhanced classes - any change will be detected (including to array content) and assignment operations that do not modify anything will not make the object dirty, because an object is identified as dirty by comparing its old content with the current content.

With enhanced classes - any assignment of a persistent field will make the object dirty, including assignment that doesn't change the object content, and including setting of an array field to another array reference (but not changing the content of the array with no assignment of the persistent array field).

Some general tips:

  • You should prefer collections (e.g. ArrayList) over arrays except for storing data (such as images) in byte[].
  • Enhancement is usually more efficient (even though assignment that doesn't change the content makes the object dirty).
ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)
#5

Thanks for the info.  So to be absolutely clear... the enhancement takes place around the assignment of a persistent field, not around the call to a setter method.  So if I want to avoid dirtying managed objects when a setter is called with the field's current value, I can test for equality inside the setter, like this:

public void setFoo(int foo) {
   if(this.foo != foo) this.foo = foo;
}

Correct?

#6

Yes, that is correct.

ObjectDB Support
ObjectDB - Fast Object Database for Java (JPA/JDO)

Post Reply

To post a reply and/or subscribe to update notifications - please login