Skip to main content

Object IDs and Orefs

What's the difference between an object ID and an oref?

An object ID is a permanent identifier for a stored object. It never changes and can always be used to locate or delete an object in the database.

An oref, on the other hand, is a temporary identifier that is used to access an object in memory. When an object is removed from memory, its oref is no longer valid. If the same object is subsequently brought back into memory, it will (in all likelihood) have a different oref.

Speaking of removing objects from memory, when we are done with this Show object it will be automatically removed from memory when all the variables referring to it go out of scope.

ClassMethod AddShow(ShowID As %String)
    // Get an order object
    If $data(%session.Data("Order")) {
        Set ord = ##class(Cinema.TicketOrder).%OpenId(%session.Data("Order"))
    Else {
        Set ord=##class(Cinema.TicketOrder).%New()
        // ...

    // Create a new TicketItem object
    Set itm = ##class(Cinema.TicketItem).%New()

    // Connect to a Show object
    Set shw = ##class(Cinema.Show).%OpenId(ShowID)
    Set itm.Show = shw
    // Connect to the Order object
    Set itm.TicketOrder = ord

    // ...
    // Save incomplete order and remember its Id in %session.
    Do ord.%Save()
    Set %session.Data("Order") = ord.%Id()
FeedbackOpens in a new tab