Maybe annotations aren’t that bad after all

19 Jan, 2009
Xebia Background Header Wave

Two years ago I blogged about annotations and that I considered them to be A Bad Thing. It seems I will have to eat my words. I am actually using them to the hilt in my current project.
We use JPA and specific Hibernate annotations on our entities. See for example these annotations on a field:
@OneToMany(mappedBy = "changePlan", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@org.hibernate.annotations.Cascade(value = org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@org.hibernate.annotations.Sort(type = org.hibernate.annotations.SortType.COMPARATOR,
comparator = PositionableComparator.class)
private SortedSet steps = new TreeSet(new PositionableComparator());

We use JUnit 4 and Spring 2.0 annotations on our test classes:
@ContextConfiguration(locations = { "/spring/ad-core-context-common.xml",
"/spring/ad-core-context-test.xml", "/spring/ad-cli-context-test.xml" })
@TransactionConfiguration(defaultRollback = true)
@TestExecutionListeners(value = { TransactionalTestExecutionListener.class,
DependencyInjectionTestExecutionListener.class })
public class AutomatedDeploymentServicesComponentTest {

And we even introduced our own annotation to describe the fields in our database:
@ConfigurationItemProperty(required = true,
description = "Name of the WebSphere Application Server. Valid examples; MyServer, Server01")
private String name;

It turns out that while annotations may clutter your code and may bind your code to specific framework etc., they do have a few useful properties:

  • Annotations relieve you from having to write (more) XML metadata, such as Hibernate mapping files and Spring context files.
  • Annotations allow people reading the code to immediately see the metadata, without having to hunt for it in other files.
  • Annotations are automatically moved when you refactor code.
  • Annotations are automatically removed when you remove code.
  • When you rename a class or a field, there is no need to change the external metadata.

In short, annotations reduce the distance between your code and its metadata. And that is A Good Thing. 😉
And as for the disadvantages I mentioned in my blog:

  • Needing the annotation classes to compile your code doesn’t seem so bad. Why wouldn’t you have the frameworks you use in your classpath?
  • So the syntax isn’t that great? It’s not as bad as XML. And at least Eclipse provides nice syntax colouring.
  • Most of the configuration data stored in annotations is pretty static, so recompiling when it changes it not that bad. For example, how often do you remap your entities without also releasing a new version of your software?
  • Using the same classes with multiple configurations is still a problem. For example, JPA also has the orm.xml file to supply/override the entity configuration. But that is not a generic solution.

So I guess the real conclusion here is that broad generalizations may embarrass you later. As always, It Depends.


Get in touch with us to learn more about the subject and related solutions

Explore related posts