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 />

// Open person "10"<br />

Set obj = ##class(Sample.Person).%OpenId(10)<br />

Write obj.%ClassName(),!<br />

// Sample.Person<br />

// Open person "110"<br />

Set obj = ##class(Sample.Person).%OpenId(110)<br />

Write obj.%ClassName(),!<br />

// Sample.Employee<br />

Note that the %OpenId method for the Sample.Employee class will not return an object if<br />

we try to open ID 10:<br />

// Open employee "10"<br />

Set obj = ##class(Sample.Employee).%OpenId(10)<br />

Write $IsObject(obj),! // 0<br />

// Open employee "110"<br />

Set obj = ##class(Sample.Employee).%OpenId(110)<br />

Write $IsObject(obj),! // 1<br />

This is because the %OpenId method for the Sample.Employee will only return instances<br />

of Sample.Employee (or its subclasses). In the above example, 10 refers to an instance of<br />

Sample.Person and cannot be opened as a Sample.Employee object.<br />

For more information on polymorphism and persistent objects see the section on Extents.<br />

12.2.2.2 Multiple Calls to %OpenId<br />

If %OpenId is called multiple times within a <strong>Caché</strong> process for a specific persistent object<br />

ID, only one object instance is created in memory: all subsequent calls to %OpenId will<br />

return a reference to the object already loaded into memory.<br />

This is demonstrated in the following example:<br />

' open and modify Person 1 in memory<br />

personA = OpenId Sample.Person(1)<br />

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

' open Person 1 "again"<br />

personB = OpenId Sample.Person(1)<br />

PrintLn "NameA: " & personA.Name<br />

PrintLn "NameB: " & personB.Name<br />

12.2.2.3 Swizzling<br />

If you open (load into memory) an instance of persistent object, and use a persistent object<br />

it references, then this referenced object is automatically opened. This process is referred to<br />

as swizzling.<br />

104 <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!