Java Reference
In-Depth Information
Querying for an abstract superclass can be inefficient because the application
must either execute a SQL SELECT statement for each table or use the SQL UNION
operator, which is only supported by some databases and can be inefficient.
Maintenance is more difficult since columns correspond to fields in super-
classes are duplicated in each table. If you add a field to a superclass, you
have to add a column to multiple tables.
My preference is to use the table-per-hierarchy approach whenever possible, and
it is the approach used by the examples in this topic. However, you should use the
table-per-class approach if the table-per-hierarchy approach would result in too
many unused or null columns.
Deciding how the domain model maps to the database schema is one part of
solving the impedance mismatch between the object-oriented and relational
worlds. You must also deal with object lifecycle and identity issues.
4.1.4
Managing object lifecycles
In addition to mapping a domain model to the database, you have to deal with the
impact of persistence on an object's lifecycle. Let's first look at the lifecycle of a
nonpersistent Java object. A Java object comes into existence when the application
calls new or invokes a constructor via reflection. After creating an object, the appli-
cation can then invoke its methods and access its fields. Because Java has a gar-
bage collector, an application does not explicitly destroy an object. Instead, the
application stops referencing the object, which is eventually destroyed by the gar-
bage collector.
The creation and destruction of a persistent object needs to be handled differ-
ently because the database is involved. When a persistent object is created, the
application must execute a SQL INSERT statement to insert a row. Similarly, in
order to delete a persistent object the application must execute a SQL DELETE
statement to remove the row from the database. As I describe in a moment, an
ORM framework will often persist and delete persistent objects automatically.
Sometimes, however, an application must persist and delete an object by calling
an ORM framework API .
4.1.5
Persistent object identity
Persistence does not just affect an object's lifecycle. Another issue to consider
when mapping a domain model to a database schema is dealing with the identity
of persistent objects. A persistent object has both Java identity and database iden-
tity, which are two very different concepts. Java defines the == operator, which
 
 
 
 
 
 
Search WWH ::




Custom Search