Java Reference
In-Depth Information
The application does not explicitly call an
API
method to save an updated
object. Instead, the
ORM
framework tracks which objects have been changed by
the application and automatically updates the database. Moreover, some
ORM
frameworks automatically save or delete an object without the application calling
an
API
method. They automatically save a nonpersistent object in the database if it
is referenced by another persistent object. An application only needs to save top-
level “root” objects that are not referenced by any other persistent objects. Simi-
larly, some
ORM
frameworks can be configured to automatically delete a child
object when its parent is deleted or when it is no longer referenced by its parent.
Query language
In addition to loading objects individually, an application often needs to execute
queries that find all matching objects. To do this, it uses the
ORM
framework's
query language. A query is expressed in terms of objects, their attributes, and rela-
tionships. The query language supports sorting and aggregate functions such as
sum()
,
min()
, and
max()
. When called by a repository to execute a query, the
ORM
framework translates the query into a
SQL
SELECT
statement that retrieves the
objects. Some
ORM
frameworks also allow the application to retrieve objects using
a
SQL
query, which is useful when it needs to use database-specific
SQL
features.
Support for transactions
An application must usually update a database using transactions in order to pre-
serve data integrity. Transactions (which are described in more detail in chapter 12)
ensure, among other things, that if the application fails partway through updating
the database the already made changes will be undone. An
ORM
framework sup-
ports transactions in a couple of ways. It is integrated with the
JTA
, which enables
it to be used by applications that update multiple resources, such as a database and
JMS
, at the same time. An
ORM
framework also has an
API
for managing transactions
directly. The
API
provides methods for beginning, committing, and rolling back a
transaction. An application can use this transaction management
API
instead of
JTA
if it accesses a single database using the
ORM
framework.
Lazy and eager loading
As we will see in later chapters, the business tier typically handles a request by first
calling the
ORM
framework to explicitly load one or more “root” objects and then
navigating to other objects by traversing relationships starting from the root objects.
For instance, the example application loads a
PendingOrder
and then navigates to
its line item and its restaurant. An important way to improve performance is to opti-
mize the loading of objects by using the right balance of lazy and eager loading.
Search WWH ::
Custom Search