User's Manual

Chapter 10. Kernel Tutorial 113
}
...
}
(1) These constructors are used to create an empty data object. The Session class is used look up
the metadata that defines the object type and returns the data object. Because the data object is a
private member variable, it is denoted m_dataObject.
(2) These two constructors are used to create a DomainObject when a data object already exists.
The OID-based constructor searches for a data object with the given OID. If the object cannot
be found, a DataObjectNotFound exception is thrown. Otherwise, the DomainObject is initial-
ized with the given data object. The data-object-based constructor is used if the data object is
available, so that an additional one does not need to be retrieved from the database.
(3) The initialize() is called after the domain object instance is created. One use of this function
is to validate the data object type to see if it is the same type or a subtype of the base data object
type. If not, an exception is thrown. In order for this to work, your subclass must implement
getBaseDataObjectType(), as in the Notes example. Subclasses of DomainObject can add
their own initialization logic, but should always call super.initialize() in the first line of
the function so that initializations from the superclasses are processed.
Example 10-3. Implementing Constructors
10.2.1.3. Adding Create Methods
The constructors described in Section 10.2.1.2 Constructors are intended to be used for creating empty
data objects or retrieving existing data objects. However, in some situations, it is useful to have a
method that takes in a set of application-specific parameters and creates a domain object for you:
/**
* Creates a new note and sets the title, body, and theme.
*
* @param title The title describing the note.
* @param body The body of the note.
* @param theme The theme of the note.
*/
public static Note create(String title, String body, NoteTheme theme) {
Note note = new Note();
note.setTitle(title);
note.setBody(body);
note.setTheme(theme);
return note;
}
This method is static, so it can be called without an instance of a Note. It constructs a new note and
then sets the title, body, and theme properties to the values passed into the method. It then returns the
constructed Note object. The Notes data is not saved in the database by the constructor. Such data is
not persisted until the save method is executed.