Pregunta

I have a base class which is a Riak entity, and some derived classes that extends BaseEntity. BaseEntity has a field named Identifier which is annotated as @RiakKey.

but apparently this is not enought. It seems that I must have the Identifier field, with the @RuiakKey anotation in each derived class, otherwise I get this exception:

com.basho.riak.client.convert.NoKeySpecifedException at com.basho.riak.client.bucket.DefaultBucket.fetch(DefaultBucket.java:535) at com.att.cso.omss.datastore.riak.controllers.RiakBaseController.isEntityExist(RiakBaseController.java:130) at com.att.cso.omss.datastore.riak.controllers.RiakBaseController.createEntity(RiakBaseController.java:94) at com.att.cso.omss.datastore.riak.controllers.RiakBaseController.createServiceProvider(RiakBaseController.java:234) at com.att.cso.omss.datastore.riak.App.serviceProviderTests(App.java:62) at com.att.cso.omss.datastore.riak.App.main(App.java:38)

So, my current implementation looks like this (duplication of the identifier field):

public class BaseEntity{

@RiakKey @JsonProperty("Id") protected String identifier;

public String getIdentifier() {
    return identifier;
}

public void setIdentifier(String identifier) {
    this.identifier = identifier;
}

. . .

public class Service extends BaseEntity{

@RiakKey 
@JsonProperty("Id")
protected String identifier;

public String getIdentifier() {
    return identifier;
}

public void setIdentifier(String identifier) {
    this.identifier = identifier;
}

is there a way to avoid this duplication?

¿Fue útil?

Solución

UPDATED: Thanks to a contribution by someone who saw this question, this is will now be supported as of the 1.0.7 client release. See: https://github.com/basho/riak-java-client/pull/180

Original Answer:

Short answer: No, there's not a way around it currently.

The reason is the com.basho.riak.client.convert.reflect.AnnotationScanner class, how it looks for these annotations, and what we allow the scoping of the fields to be.

It uses Class.getDeclaredFields() which only gets the fields explicitly declared in the class, not inherited ones. The reason for this is that it gets private and protected members, whereas Class.getFields() would get inherited ones but only if they were declared public in a parent class.

One simple way around this would be to recursively scan each parent class up the inheritance tree. Because of how we cache the annotated fields for domain objects this would only be a one time hit and probably wouldn't be too terrible of a thing to do.

If this is something you'd be interested in having added to the client, please feel free to open an issue on github (or code & submit it yourself, of course - we're always thankful for community submissions!).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top