25.01.2015 Views

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

Using Caché Objects - InterSystems Documentation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

find the object within the database. Once assigned, you cannot alter the Object ID value for<br />

a specific object instance (even if it is a user-provided ID).<br />

You can find the Object ID value for an object that has been saved using the %Id method:<br />

// Open person "22"<br />

Set person = ##class(Sample.Person).%OpenId(22)<br />

Write "Object ID: ",person.%Id(),!<br />

In more detail, the %Save method does the following:<br />

The Persistence Interface<br />

1. First it constructs a temporary structure known as a “SaveSet.” The SaveSet is simply<br />

a graph containing references to every object that is reachable from the object being<br />

saved. The purpose of the SaveSet is to make sure that save operations involving complex<br />

sets of related objects are handled as efficiently as possible. The SaveSet also resolves<br />

any save order dependencies among objects.<br />

As each object is added to the SaveSet, its %OnAddToSaveSet callback method is<br />

called, if present.<br />

2. It then visits each object in the SaveSet in order and checks if they are modified (that is,<br />

if any of their property values have been modified since the object was opened or last<br />

saved). If an object has been modified, it will then be saved.<br />

3. Before being saved, each modified object is validated (its property values are tested; its<br />

%OnValidateObject method, if present, is called; and uniqueness constraints are tested);<br />

if the object is valid, the save proceeds. If any object is invalid, then the call to %Save<br />

fails and the current transaction is rolled back.<br />

4. Before and after saving each object, the %OnBeforeSave and %OnAfterSave callback<br />

methods are called, if present.<br />

These callbacks are passed an Insert argument which indicates whether an object is being<br />

inserted (saved for the first time) or updated.<br />

If either of these callback methods fails (returns an error code) then the call to %Save<br />

fails and the current transaction is rolled back.<br />

If the current object is not modified, then %Save does not write it to disk; it returns success<br />

because the object did not need to be saved and, therefore, there is no way that there could<br />

have been a failure to save it. In fact, the return value of %Save indicates that the save<br />

operation either did all that it was asked or it was unable to do as it was asked (and not<br />

specifically whether or not anything was written to disk).<br />

<strong>Using</strong> <strong>Caché</strong> <strong>Objects</strong> 101

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!