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.

Object Persistence<br />

1. Its primary (first) superclass must either be %Persistent or some other persistent class,<br />

and,<br />

2. It must specify that its ClassType is persistent.<br />

For example:<br />

Class MyApp.MyClass Extends %Persistent [ClassType = persistent]<br />

{<br />

}<br />

If you omit the ClassType = persistent or if %Persistent (or a subclass) is not the first<br />

class in the superclass list, then your class will not be persistent.<br />

12.2 The Persistence Interface<br />

The %Persistent class defines a set of methods, known as the Persistence Interface, that provides<br />

the means to work with object persistence. Among other things, the Persistence Interface<br />

provides the ability to save objects to the database, load objects from the database, delete<br />

objects, and test for existence. These are described in the following sections.<br />

12.2.1 Saving <strong>Objects</strong><br />

To save a persistent object to the database, invoke its %Save method:<br />

Set obj = ##class(MyApp.MyClass).%New()<br />

Set obj.MyValue = 10<br />

Set sc = obj.%Save()<br />

The %Save method returns a %Status value that indicates whether the save operation succeeded<br />

or failed (such as if it has invalid property values or violates a uniqueness constraint).<br />

Calling %Save on an object automatically saves all modified objects that can be “reached”<br />

from the object being saved: that is, all embedded objects, collections, streams, referenced<br />

objects, and relationships involving the object are automatically saved if needed. The entire<br />

save operation is carried out as one transaction: if any object fails to save, then the entire<br />

transaction fails and rolls back (no changes are made to disk; all in-memory object values<br />

are what they were prior to calling %Save).<br />

When an object is saved for the first time, the %Save method automatically assigns it an<br />

Object ID value (the ID is generated using the $Increment function unless the class is using<br />

a user-provided Object ID based on property values using an idkey index) that is used to later<br />

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

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

Saved successfully!

Ooh no, something went wrong!