Java Reference
In-Depth Information
These files can be created directly with a text editor or with the help of various tools cre-
ated by the Hibernate team and others. We discuss the details of XML mapping files in
Chapter 8.
Hibernate now also supports the Annotations feature introduced in Java 5. This permits
the use of a special syntax to include metadata directly in the source code for the application.
While this allows the core features of Hibernate to be controlled, many of the additional fea-
tures cannot be specified in annotations. There is therefore something of a trade-off between
the advantages of maintaining the mapping information directly within the associated source
code, and the more flexible features available from the XML-based mappings. We discuss the
details of annotation-based mapping in Chapter 6.
Other Information Represented in Mappings
While Hibernate can determine a lot of sensible default values for the mappings, most of
these can be overridden by one or both of the file- and XML-based approaches. Some apply
directly to mapping; others, such as the foreign key names, are really only pertinent when
the mapping is used to create the database schema. Lastly, some mappings can also provide
a place to configure some features that are perhaps not “mappings” in the purest sense. The
final sections of this chapter discuss the features that Hibernate supports in addition to
those already mentioned.
Specification of (Database) Column Types and Sizes
Java provides the primitive types and allows user declaration of interfaces and classes to
extend these. Relational databases generally provide a small subset of “standard” types, and
then provide additional proprietary types.
Restricting yourself to the proprietary types will still cause problems, as there are only
approximate correspondences between these and the Java primitive types.
A typical example of a problematic type is java.lang.String (treated by Hibernate as
if it were a primitive type since it is used so frequently), which by default will be mapped to
a fixed-size character data database type. Typically, the database would perform poorly if a
character field of unlimited size were chosen—but lengthy String fields will be truncated
as they are persisted into the database.
By overriding the default type mappings, the developer can make appropriate trade-offs
between storage space, performance, and fidelity to the original Java representation.
The Mapping of Inheritance Relationships to the Database
There is no SQL standard for representing inheritance relationships for the data in tables; and
while some database implementations provide a proprietary syntax for this, not all do. Hiber-
nate provides several configurable ways in which to represent inheritance relationships, and
the mapping file permits users to select a suitable approach for their model.
Search WWH ::




Custom Search