Java Reference
In-Depth Information
Primary Key
Hibernate demands that a primary key be used to identify entities. The choice of a surrogate
key, a key chosen from the business data, and/or a compound primary key can be made via
the mapping file.
When a surrogate key is used, Hibernate also permits the key-generation technique to be
selected—from a range of techniques that vary in portability and efficiency.
The Use of SQL Formula-Based Properties
It is sometimes desirable that a property of an entity should be maintained not as data directly
stored in the database, but rather as a function performed on that data—for example, a sub-
total field should not be managed directly by the Java logic, but instead maintained as an
aggregate function of some other property.
Mandatory and Unique Constraints
As well as the implicit constraints of a primary or foreign key relationship, you can specify that
a field must not be duplicated—for example, a username field should often be unique.
Fields can also be made mandatory—for example, requiring a message entity to have both
a subject and message text.
The generated database schema will contain corresponding NOT NULL and UNIQUE constraints
so that it is literally impossible to corrupt the table with invalid data (rather, the application logic
will throw an exception if any attempt to do so is made).
Note that primary keys are implicitly both mandatory and unique.
Cascading of Operations
As alterations are made to the object model, operations on some objects should cascade
through to related objects. For example, deleting a stocked item should perhaps cause any
associated catalog entries to be deleted. The reverse—deleting a single catalog entry—should
not necessarily cause the stocked item to be deleted from the database entirely!
It would be awkward to manage the appropriate cascading rules from code alone, so
cascading rules can be specified at a fine level of detail within the mappings.
Summary
This chapter has given you an overview of the reason why mapping files are needed, and what
features they support beyond these absolute requirements. It has discussed the various types
of associations, and the circumstances under which you would choose to use them.
The next two chapters look at how mappings are specified using annotations and XML
files respectively.
Search WWH ::




Custom Search