Skip to main content

Objects: Creating, Opening, and Persisting

Once you have a reference to a CacheDatabase object, you can create new objects, open existing objects, and save new or changed objects to the database using projections.

To create new objects use the constructors contained in the Java projection class. A constructor creates an instance of the Java projection class on the Java client as well as an instance of the corresponding Caché class on the Caché server. Creating an instance does not automatically save it to the database. Caché does not assign it an object ID until the object is saved. The constructor requires a reference to a CacheDatabase object as an argument. The following code invokes the constructor for the Contact class:


Contact contact = new Contact(db);

To open a saved object, that is bring it into memory both on the Java client and the Caché server, use the _open method of the object's projection class. Select the object to open using its object ID. The following code opens the Contact instance with object ID 1. Notice that the object must be explicitly cast to the appropriate type.


Contact contact = (Contact)(Contact._open(db, new Id(1)))

To save a Caché object to the database invoke the save method of its Java projection. If the object has not yet been saved, Caché assigns it an object ID. Executing a projection's save method causes the %Save method of the corresponding Caché object to execute. The save method returns 1 if the operation is successful.


int success = contact.save();

Note:

All of the above methods throw checked exceptions of type CacheException.

To learn more about object IDs in Caché, read Identifiers for Saved Objects: ID and OID in Using Caché Objects.

FeedbackOpens in a new tab