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.

For example, the following code opens an instance of Sample.Employee object and automatically<br />

(swizzles) opens its related Sample.Company object by referring to it using dot syntax:<br />

// Open employee "101"<br />

Set emp = ##class(Sample.Employee).%OpenId(101)<br />

// Automatically open Sample.Company by referring to it:<br />

Write "Company: ",emp.Company.Name,!<br />

When an object is swizzled, it is opened using the default concurrency value of Atomic (see<br />

next section). If you wish to change the concurrency level of a swizzled object use the<br />

%UpgradeConcurrency and %DowngradeConcurrency methods.<br />

A swizzled object is removed from memory as soon as no objects or variables refer to it.<br />

12.2.2.4 Concurrency<br />

The %OpenId method takes an optional concurrency argument as input. This argument<br />

specifies the concurrency level (type of locks) that should be used to open the object instance.<br />

For more information on the possible object concurrency levels, refer to Object Concurrency.<br />

If the %OpenId method is unable to acquire a lock on an object, it will fail.<br />

You can raise or lower the current concurrency setting for an object using the %Persistent<br />

class' %UpgradeConcurrency and %DowngradeConcurrency methods, respectively.<br />

12.2.2.5 Reloading an Object from Disk<br />

If you wish to reload an in-memory object with the values stored within the database, you<br />

can use the %Reload method provided by the %Persistent class.<br />

// Open person "1"<br />

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

Write "Original value: ",person.Name,!<br />

// modify the object<br />

Set person.Name = "Black,Jimmy Carl"<br />

Write "Modified value: ",person.Name,!<br />

// Now reload the object from disk<br />

Do person.%Reload()<br />

Write "Reloaded value: ",person.Name,!<br />

12.2.2.6 Reading Stored Values<br />

The Persistence Interface<br />

Suppose you have opened an instance of a persistent object, modified its properties, and then<br />

wish to view the original value stored in the database before saving the object. The easiest<br />

way to do this is to use an SQL statement (SQL is always executed against the database; not<br />

against objects in memory).<br />

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

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

Saved successfully!

Ooh no, something went wrong!