cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From devnull2...@gmx.de
Subject check for changes before commit
Date Wed, 21 Nov 2012 20:02:24 GMT
Hi,

I'm working on web application with tapestry 5.3.6, tapestry5-cayenne 0.5-SNAPSHOT and cayenne
3.1B1.

I've got several pages with forms for a user to edit something. Some
entities/objects have a field called lastmodified, so before I call
context.commitChanges() I set a new timestamp for lastmodified. Before I do that I'd like
to find out if there really are changes. Otherwise, if a user doesn't actually edit anything
and just clicks on save, I'd set a new
lastmodified date and that timestamp would be the only value that really is
different than before.

I can't use context.hasChanges() because it is always true. Tapestry calls setXyz() for every
field of a bean (at least if I use BeaneditForm). 

So I'm wondering if I should not use BeanEditForm at all and instead use forms with input
fields that aren't bound to the object's values and handle everything myself? For example
use a String name for a form input field and then check the user's input, compare the value
to myItem.getName() and only if it's different call myItem.setName(name)? Then I could check
context.hasChanges I guess.

I googled around some and found a thread from 2009 with a similar problem: Any way to check
if object has really changed?
http://mail-archives.apache.org/mod_mbox/cayenne-user/200903.mbox/%3C3219fff70903240659p2196e7d2h7c9d020cc50635fa@mail.gmail.com%3E
The thread starter also posted some code to check for changes.
Should I rather use this method?
What would be "good practice"?

I also don't understand all of the code from that thread I mentioned. 

private boolean hasChanged(CayenneDataObject cdo) {
    ...
    if (cdo.getPersistenceState() != PersistenceState.MODIFIED) {
        return true;
    }
    ...
}

It checks if PersistenceState is MODIFIED and if it is not, then it returns true. But what
if PersistenceState is COMMITTED or HOLLOW, this doesn't necessarily mean there are changes,
does it?

Further down the relationships are checked:

for (ObjRelationship rel : entity.getRelationships()) {
    if (!rel.isFlattened() && !rel.isToMany()) {
        ...
    }
}

but there is no else for the if, so toMany relationships are ignored. How could I check if
a toMany relationship of an entity has changes? In this  case I sometimes should set a new
lastmodified timestamp, too.

Cheers,
-Bjello






Mime
View raw message