Skip to main content

Identifiers for Saved Objects: ID and OID

Identifiers for Saved Objects: ID and OID

When you save an object for the first time, the system creates two permanent identifiers for it, either of which you can later use to access or remove the saved objects. The more commonly used identifier is the object ID. An ID is a simple literal value that is unique within the table. By default, the system generates an integer to use as an ID.

An OID is more general: it also includes the class name and is unique in the database. In general practice, an application never needs to use the OID value; the ID value is usually sufficient.

The %PersistentOpens in a new tab class provides methods that use either the ID or the OID. You specify an ID when you use methods such as %OpenId(), %ExistsId(), and %DeleteId(). You specify the OID as the argument for methods such as %Open(), %Exists(), and %Delete(). That is, the methods that use ID as an argument include Id in their names. The methods that use OID as the argument do not include Id in their names; these methods are used much less often.

When a persistent object is stored in the database, the values of any of its reference attributes (that is, references to other persistent objects) are stored as OID values. For object attributes that do not have OIDs, the literal value of the object is stored along with the rest of the state of the object.

Projection of Object IDs to SQL

The ID of an object is available in the corresponding SQL table. If possible, Caché uses the field name ID. Caché also provides a way to access the ID if you are not sure what field name to use. The system is as follows:

  • An object ID is not a property of the object and is treated differently from the properties.

  • If the class does not contain a property named ID (in any case variation), then the table also contains the field ID, and that field contains the object ID. For an example, see the previous section.

  • If the class contains a property that is projected to SQL with the name ID (in any case variation), then the table also contains the field ID1, and this field holds the value of the object ID.

    Similarly, if the class contains properties that are projected as ID and ID1, then the table also contains the field ID2, and this field holds the value of the object ID.

  • In all cases, the table also provides the pseudo-field %ID, which holds the value of the object ID.

The OID is not available in the SQL table.

Object IDs in SQL

Caché enforces uniqueness for the ID field (whatever its actual name might be). Caché also prevents this field from being changed. This means that you cannot perform SQL UPDATE or INSERT operations on this field. For instance, the following shows the SQL needed to add a new record to a table:

INSERT INTO PERSON (FNAME, LNAME)VALUES (:fname, :lname)

Notice that this SQL does not refer to the ID field. Caché generates a value for the ID field and inserts that when it creates the requested record.

FeedbackOpens in a new tab