0
votes

I am using datastax cassandra mapper and accessor to read/write data to cassandra.

      <dependency>
       <groupId>com.datastax.cassandra</groupId>
       <artifactId>cassandra-driver-mapping</artifactId>
       <version>2.1.5</version>
      </dependency>

I have entities which has common fields which I have put in Super class. But the problem is datastax AnnotationParser just do entityClass.getDeclaredFields() and does not take care of superclass fields. Hence when I read a record from DB, the returned entity does not have all the fields populated.

Is there any fix or workaround available for this? Last option is to get rid of super class and add fields to individual entity class. But I don't want to do that as it's against OOPS concepts and gonna make code non maintainable.

Appreciate any help. Thanks.

1

1 Answers

0
votes

Its known issue with Datastax Mapper. https://datastax-oss.atlassian.net/browse/JAVA-541

As a workaround I created custom MappingManager and AnnotationParser class. I kept the code exactly same as Datastax provided MappingManager.java and AnnotationParser.java. The only change I did is, in AnnotationParser wherever getDeclaredFields() was getting called, I replaced it with following call. And used my own AnnotationParser in custom Mapping manager.

  public static ArrayList<Field> getAllFields(Class clazz) {
return getAllFieldsRec(clazz, new ArrayList<Field>());
}

private static ArrayList<Field> getAllFieldsRec(Class clazz, ArrayList<Field> fields) {
Class superClazz = clazz.getSuperclass();
if (superClazz != null) {
  getAllFieldsRec(superClazz, fields);
}
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
return fields;
}

I have one single point in code where Mapping manager is initialized.

protected MappingManager getMappingManager() {
return new CustomMappingManager(getSession());
}

So in future if we get latest datastax mapper with this issue fixed, we will have to just delete our two classes and change code at only one place.

Thought of posting it, so it might help someone.