beautypg.com

Apple WebObjects 3.5 User Manual

Page 84

background image

Chapter 3

Creating a WebObjects Database Application

84

1. Inspect the middle image button.

Its

action

attribute is bound to the action method

saveChanges

.

2. Look in the

Main.java

class to see how

saveChanges

is implemented.

The method (shown below with comments omitted) simply saves any
changes that have been made to

movieDisplayGroup

’s objects to the database.

public void saveChanges() throws Exception {

try {

this.session().defaultEditingContext().saveChanges();

}

catch (Exception exception) {

System.err.println("Cannot save changes ");

throw exception;

}

}

this.session()

returns a Session object that represents a connection to the

application by a single user. A Session object provides access to an
EditingContext object. The expression

this.session().defaultEditingContext().saveChanges();

sends a

saveChanges

message to the Session’s

defaultEditingContext

. This default

EditingContext object manages graphs of objects fetched from the
database, and all changes to the database are saved through it. For more
information, see the EditingContext class specification in the Enterprise
Objects Framework Reference
.

An EditingContext’s

saveChanges

method uses other Enterprise Objects

Framework objects to analyze its network of enterprise objects (Movie
objects referenced by the application) for changes and then to perform a set
of corresponding operations in the database. If an error occurs during this
process,

saveChanges

throws an exception.

Main.java

’s

saveChanges

method simply

raises the exception, having the effect of returning a diagnostic page. You
could return an error page that explains the reason for the save failure
instead, but the application in this tutorial uses the default behavior.

3. Inspect the first and third image buttons to see what their

action

attributes are

bound to.

They are bound to

movieDisplayGroup.insert

and

movieDisplayGroup.delete

,

respectively. The DisplayGroup

insert

method creates a new enterprise

object, then inserts it into the display group’s list of objects just past the
current selection. The DisplayGroup

delete

method deletes the display