Allow spring data couchbase annotations to be used in meta-annotation context [DATACOUCH-495]
Garret Fick opened DATACOUCH-495 and commented
As a developer, I want to be able to easily choose between using Couchbase or other simplar repositories that all use the same document and change the implementing repository at runtime, for example by profile configuration. This can be achieved by adding annotations required by each repository implementation to each field in the document.
For example, if I wanted to support both Couchbase and DynamoDB, I might have the following
class Entity {
@Id
@DynamoDBHashKey
@DynamoDBAutoGeneratedKey
public String id;
@Field
@DynamoDBAttribute
public String field1;
@Field
@DynamoDBAttribute
public String field2;
}
The annotation on the field is repetitive. (The same idea applies to the ID if I have multiple entity definitions).
To eliminate the repetition and reduce the opportunity for defects, I would like to be able to define my own annotation, for example:
@Id
@DynamoDBHashKey
@DynamoDBAutoGeneratedKey
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.FIELD })
public @interface MyId {}
AND
@Field
@DynamoDBAttribute
public @interface MyField {}
Then, I an write the simpler
class Entity {
@MyId
public String id;
@MyField
public String field1;
@MyField
public String field2;
}
This is currently not possible because the target for the annotations only permits fields, but it is trivial to change.
NOTE While this is not my current use case, it would also be possible to use this capability to create combinations that include JSR 380 annotations (for example @NotNull), again for the objective of not repeating yourself.
No further details from DATACOUCH-495
Garret Fick commented
This issue should be closed. I realised it is not in the spring data couchbase repository, but in another repository. I have created the issue there.
https://issues.couchbase.com/browse/JCBC-1544
Garret Fick commented
Going back on my original. There is still work that can be done in this context. The couchbase-java-client declares a few attributes, but spring-data-couchbase declares the Document annotation (https://github.com/spring-projects/spring-data-couchbase/blob/master/src/main/java/org/springframework/data/couchbase/core/mapping/Document.java). It would be useful to declare this in a meta-attribute
Michael Reiche commented
This may not be a bad idea, but the scope is beyond spring-data-couchbase. For the example, this would need to be supported across DynamoDB and Couchbase spring-data projects. Making some annotations common across projects would also be helpful.
As for supporting within spring-data-couchbase - spring-data-couchbase needs very few annotations. It doesn't need the @Field annotation, it does not even need the @Id annotation when there is a field named 'id' (from 4.0.5)
The couchbase @Field (and other annotations) have Target of ANNOTATION_TYPE, so you can indeed build your own annotations.
@Target({ ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE })