Search code examples
javaspringazureazure-cosmosdb

Is there a mapping for attribute name inside a cosmosDB java Entity?


I have an entity class for Dynamo that I have to persist on Cosmos DB using the same application. The application is using a DynamoDB library that allows attributes to be renamed (mapped) to different attribute names in the collection.

@Getter
@Setter
class DynamoEntity {
  @DynamoDBAttribute(attributeName = "attr_inside_collection")
  private String attr;
}

I have to persist the same object inside Cosmos, and I'm using azure-spring-data-cosmos v 5.5. However, they don't have any mapping of that kind. They do have mappings for @PartitionKey and @CompositeIndex, but I couldn't find any annotation that would overwrite the attribute name.

I specifically need the objects inside the DB to be stored with the same attr names as they're stored in Dynamo, however, there is a mapping for the HTTP/gRPC requests on that entity that translate the ugly snake case attr names to readable request/response jsons.

I will need a separate Entity class for Cosmos, I know, because I will need to use it in the cosmos library for queries. BUT I wish I could keep the nice attr names. The way I'm doing now, it's ugly, because I have to mirror the attribute names of the collection on the entity like that:

@Getter
@Setter
class CosmosEntity {
  @Id
  @PartitionKey
  private String differntIdName; // It reads 'id' from collection

  private String attr_inside_collection; // It needs the same attr name inside collection
}

Amazingly, it is able to detect a different attribute name for @Id, but the others don't work. I tried @JsonProperty and extending serializable but it also didn't work.

Is there a way in which the Cosmos Entity have different attribute names from the ones stored in the Container/Collection?


Solution

  • I managed to do it with @JsonProperty!.

    I was using

    import org.codehaus.jackson.annotate.JsonProperty;
    

    ... but apparently it wasn't working as I mentioned in the post. I switched to:

    import com.fasterxml.jackson.annotation.JsonProperty;
    

    and now it works just fine ;)