Java Reference
In-Depth Information
Listing 8.12. The
Order
class, which has many orders and belongs to a customer
There's a lot going on here. First, an order contains a
Set
of order lines. Orders also
belong to a specific customer. The customer reference implies that you can navigate from
an order to its associated customer. By assigning it to the
belongsTo
property in this
way, a cascade-delete relationship exists between the two classes. If a customer is deleted
from the system, all of its orders are deleted as well.
Grails belongsto
In Grails, the word
belongsTo
implies a cascade-delete relationship.
The
getPrice
method computes the price of the order by summing up the prices on each
order line. It too is a derived quantity and is therefore not saved in the database.
The
dateCreated
and
lastUpdated
properties are automatically maintained by
Hibernate. When an order is first saved, its
dateCreated
value is set; and every time it's
modified,
lastUpdated
is saved as well.