PATH |
Describes how to save changes made to the enterprise objects managed by an EOEditingContext.
Changes to enterprise objects are not reflected in the database until you invoke the saveChanges method on the EOEditingContext that manages the objects. In a WebObjects application, the session's default editing context generally manages all of the enterprise objects in that session. To get the default editing context from within a component use
editingContext = this.session().defaultEditingContext();
To save your changes from your user interface, you need to create an action method that invokes saveChanges as shown below.
public WOComponent saveChanges() { this.session().defaultEditingContext().saveChanges(); return null; }
You can bind this method to the action attribute of a user interface dynamic element like WOHyperlink using WebObjects Builder.
Your save can fail and raise an exception for many reasons. For example, the user might enter insufficient data, or the database might be down. When an exception is raised, the default WebObjects behavior is to display a page containing debugging information. This information is useful for the developer but inappropriate for the end user. The following code shows how to catch a save exception so you can present the user with an informative error message. This code is functionally identical to the code generated by the database wizard.
public void saveChanges() throws Exception { try { this.session().defaultEditingContext().saveChanges(); } catch (Exception exception) { // An error occurred during the save. You could present an error // page which explains the reason for the save failure. The default // behavior is to raise an exception which presents a diagnostic page. System.err.println("Cannot save changes "); throw exception; } }
15 July, 1998. Carmine Marino. First Draft.
26 February, 1999. Clif Liu. Second Draft.