Java Reference
In-Depth Information
F
This maps the restaurant field to the RESTAURANT_ID column, which is a foreign
key column that references the RESTAURANT table.
The lineItems field is defined to be a collection of PendingOrderLineItem
objects. The dependent-element="true" attribute specifies that line items
should be deleted when removed from the line items collection or when the
pending order is deleted. The <element> element specifies that the lineItems
field is mapped to the PENDING_ORDER_ID foreign key column of Pending-
OrderLineItem 's table (i.e., PENDING_ORDER_LINE_ITEM ). The allows-
null="false" attribute specifies that the column cannot be null . The <order>
element specifies that the position of the line item in the list is stored in the
LINE_ITEM_INDEX column of the PENDING_ORDER_LINE_ITEM table.
The metadata for the other domain classes is similar. Once you have written the
JDO XML metadata for a domain model, you must run the JDO implementation's
bytecode enhancer to modify the class files. Once you do, the application will be
able to persist instances of those classes in the database and the tests we wrote ear-
lier will pass.
At this point, you know how to persist domain objects; the next step is to write
the repositories.
G
5.3 Implementing the JDO repositories
One part of persisting a domain model with JDO is defining the O/R mapping and
writing the tests to verify that it works. We must also implement the repositories,
which define methods for creating, finding, and deleting persistent domain
objects and are the only domain model classes that call the JDO API s to manipu-
late persistent objects.
If you are using test-driven development, you begin the process of implement-
ing a repository by writing some tests for the repository. We saw in chapter 4 that
there are two kinds of tests that you need to write for a repository. First are mock
object tests that verify that the repository calls the JDO and Spring API s correctly.
Mock objects are an effective way to directly test the functionality implemented by
the repository independently of the persistence framework and the database. Sec-
ond, you need database tests for the queries that are executed by the repository.
These tests execute the queries against a database populated with test data and
verify that they return the correct results. Once you have written the tests, you
then write the code for the repository and get the tests to pass.
This section shows how to do this for the RestaurantRepository.find-
AvailableRestaurants() method, which finds the restaurants that serve a given
 
 
 
 
Search WWH ::




Custom Search