A few comments on the implementation:
1. This line should probably be called after commit changes:
o.setPersistenceState(PersistenceState.COMMITTED);
(for deleted case it should be "unregisterObjects" instead)...
2. Copy method will not work if DB columns have names different from
object properties (i.e. DbAttribute names are not the same as
ObjAttributes), as snapshot key names are using DbAttributes for naming. I
guess just iterating through attributes and doing "readProperty" /
"writeProperty" on source and destination objects would be the easiest way
to go about it.
Andrus
> It was a little bit more complicated... was talking about the general
> case. Here my code (copyleft, unstable ;-)) :
>
> public static void singleSave(DataObject o) {
> // Perform the single save
> DataContext c = DataContext.createDataContext(o.getDataContext()
> .getParentDataDomain().getName());
>
> DataObject newObject;
> switch (o.getPersistenceState()) {
> case NEW:
> newObject = c.createAndRegisterNewObject(o.getClass());
> copyModifiedAttributes(o, newObject);
> // FIXME Granted?
> o.setPersistenceState(PersistenceState.COMMITTED);
> break;
> case MODIFIED:
> newObject = DataObjectUtils.objectForPK(c, o.getObjectId());
> copyModifiedAttributes(o, newObject);
> // FIXME Granted?
> o.setPersistenceState(PersistenceState.COMMITTED);
> break;
> case DELETED:
> newObject = DataObjectUtils.objectForPK(c, o.getObjectId());
> c.deleteObject(newObject);
> break;
> default:
> throw new InvalidParameterException("Cannot singleSave() "
> + PersistenceState.persistenceStateName(o
> .getPersistenceState()) + " data objects");
> }
> c.commitChanges();
> }
>
> private static void copyModifiedAttributes(DataObject from, DataObject
> to) {
> DataContext context = from.getDataContext();
> ObjEntity entity = context.getEntityResolver().lookupObjEntity(from);
>
> Map<String, ?> committed = context.getObjectStore().getCachedSnapshot(
> from.getObjectId());
> Map<String, ?> current = context.currentSnapshot(from);
>
> for (Object it : entity.getAttributes()) {
> final String attr = ((ObjAttribute) it).getName();
>
> if (committed == null) { // Mainly for NEW objects
> to.writeProperty(attr, current.get(attr));
> } else { // For MODIFIED objects
> Object value = committed.get(attr);
> if (value != current.get(attr)) {
> to.writeProperty(attr, value);
> }
> }
> }
> }
>
> Le mercredi 27 avril 2005 à 20:19 -0400, Mike Kienenberger a écrit :
>> =?ISO-8859-1?Q?Mika=EBl?= Cluseau <nwr..wrk.dyndns.org> wrote:
>> > I'm sorry to ask that but neither google nor the cayenne doc
>> enlighted me on a stupid question... how do you save a single
>> object? (yes, I must support this feature...)
>>
>> The same way you save multiple items --
>> dataContextInstance.commitChanges();
>>
>> A simplified free-form example off the top of my head so there's
>> probably errors:
>>
>> DataContext dc = DataContext.createNewDataContext();
>> MyObject mo = dc.createAndRegisterNewObject(MyObject.class);
>> mo.setBar("foo");
>> dc.commitChanges();
>>
> --
> Mikaël Cluseau <nwr..wrk.dyndns.org>
This archive was generated by hypermail 2.0.0 : Tue May 10 2005 - 09:31:55 EDT