We have started taking advantage of Annotations (introduced in Java 1.5) all over application and especially in Hibernate. Initially, I was happy to get rid of those hbm files and bringing in Named Queries inside Java classes. This is how a table definition and a named query goes,
@NamedQueries( {@NamedQuery(name = “selectValues”, query = “select count(*) from XXX xxx”) })@Table(name = “XXX”, uniqueConstraints = @UniqueConstraint(columnNames = “XXX_ID”))
This is indeed convenient compared to having a separate hbm file to define the table and class mapping. Also, it makes more sense to have the query inside the class than writing it in some DAO class or a property file.
But, when the application started evolving, we had to make several changes to these classes. Say, if the table name changes, we should change them in this class or want to make a small change to the query, we should change them in this class. That’s when I started realizing that, refactoring a class and building again is more time consuming than making those changes in the property XML files.
So, ideally these configurations should have been existing inside an XML file. Though Annotations makes it more readable, along with it comes the headache of refactoring and rebuilding. So, when you think of Annotations next time, keep this in mind.
Here is what a Java guru has to say about Annotations,
In his case against annotations, he says that annotations are not metaData but just decorative data. Annotations are hard coded configurations that cannot replace XML. He feels that Java annotations were just meant as a response to .NET and not something that Java developers want.