DN v5.1 : Meta Annotations

With normal JDO or JPA usage you may have annotated a class like this

@Entity
@DatastoreId
public class Person { ... }

So we have a class that is JPA persistable, and is using the DataNucleus “datastore-identity” extension, providing a surrogate identity column in the datastore. This is fine, and easy enough. But what if you needed to put these 2 annotations on many classes? It would be simpler if you could define your own “composite” annotation that provided them more concisely. This is where we introduce meta-annotations.

If we define our own annotation like this

@Target(TYPE)
@Retention(RUNTIME)
@Entity
@DatastoreId
public @interface DatastoreIdEntity {}

You see that this annotation @DatastoreIdEntity provides both of our normal JPA annotations. So we can now annotate our JPA class like this

@DatastoreIdEntity
public class Person { ... }

Much simpler!

You can do the same thing with JDO annotations, as well as for annotations on fields/methods.

Note that this is new in DataNucleus v5.1, and to use the field/method level JDO/JPA annotations you will have to use updated (javax.) API jars, that will be provided with v5.1

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s