Modify Annotations in Scala

Best Organized Kitchen Ever?

Best Organized Kitchen Ever?

In Scala I rarely use annotations. However when you need to interoperate with a framework which relies on annotations you need use them. So lately I wanted to store some simple case classes with JPA. As you know, JPA relies quite heavily on annotations, unless you move everything out to XML descriptors.

I wanted to store something like this:

Unfortunately this results in exceptions like the one below. Basically the annotation is not placed on the ‘field’ but rather on the parameter of the constructor.

Now you actually can specify on which elements such an annotation is added. So in this situation we can tell the Scala to ensure that the annotation is also placed on the id field:

This will fix the issue. There are more possible targets, like Scala accessors and Java Beans getter/setters for @BeanProperty annotated fields. Take a look at the documentation here.

That’s all. Nothing more to tell =)

Tagged on: