Java Reference
In-Depth Information
Cons of Annotations
Using annotations immediately restricts your code to a Java 5 environment. This immediately
rules out the use of annotations for some developers, as some application servers do not yet
support this version of the JVM. Even when there are no technical reasons why a current JVM
could not be used, many shops are quite conservative in the deployment of new technologies.
If you are migrating from a Hibernate 2 environment or an existing Hibernate 3 environ-
ment, you will already have XML-based mapping files to support your code base. All else
being equal, you will not want to re-express these mappings using annotations just for the
sake of it.
If you are migrating from a legacy environment, you may not want to alter the preexisting
POJO source code, in order to avoid contaminating known-good code with possible bugs.
If you do not have the source code to your POJOs (because it has been lost, or because it
was generated by an automated tool), you may prefer the use of external XML-based map-
pings to the decompilation of class files to obtain Java source code for alteration.
Maintaining the mapping information as external XML files allows the mapping infor-
mation to be changed to reflect business changes or schema alterations without forcing you
to rebuild the application as a whole.
Hibernate 3 support for annotation-based mappings is not yet as mature as its support
for XML-based mapping files. For example, while you can still make appropriate foreign key
relationships for use in schema generation, you cannot generally name the foreign keys used.
Pros of Annotations
Having considered the drawbacks, there are some powerful benefits to contrast against them.
First, and perhaps most persuasively, we find annotations-based mappings to be far more
intuitive than their XML-based alternatives, as they are immediately in the source code along
with the properties that they are associated with.
Partly as a result of this, annotations are less verbose than their XML equivalents, as evi-
denced by the contrast between Listings 6-1 and 6-2.
Listing 6-1. A Minimal Class Mapped Using Annotations
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Sample {
@Id
public Integer id;
public String name;
}
Search WWH ::




Custom Search