Java Reference
In-Depth Information
CHAPTER 6
n n n
Mapping with Annotations
I n Chapter 5, we discussed the need to create mappings between the database model and the
object model. Mappings can be created as separate XML files, or as Java 5 annotations inline
with the source code for your POJOs. In this chapter, we discuss the use of annotations, and in
the next chapter, we will discuss the use of XML files.
Java 5 Features
Java 5 was introduced in late 2004 as a major new release of the language. Annotations are not
supported by versions of Java prior to this, so while core Hibernate 3 is compatible with earlier
versions, you will not be able to take advantage of the features described in this chapter unless
your development, compilation, and runtime tools support at least version 5 of the language
(version 6 of Java, codenamed Mustang, is expected some time in late 2006).
Since we must perforce assume that you have a Java 5 environment available to you, the
examples in this chapter will also take advantage of some of the other enhanced language fea-
tures introduced in Java 5, as follows:
Generics
Enhanced for loops
Static imports
Enumerations
Autoboxing
Variable parameter lists
Using these features will make the source code for this chapter noticeably more com-
pact. Similarly, annotation-based mappings are significantly terser than their XML-based
counterparts.
Creating Hibernate Mappings with Annotations
Prior to annotations, the only way to create mappings was through XML files—although
tools from Hibernate and third-party projects allowed part or all of these to be generated
from Java source code. Although using annotations is the newest way to define mappings, it
is not automatically the best way to do so. We will briefly discuss the drawbacks and benefits
of annotations before discussing when and how to apply them.
93
Search WWH ::




Custom Search