question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid class type on nested object - MappingCouchbaseConverter - couchbase transactions

See original GitHub issue

Transaction conversion on read, according to the documentation, should work with this code :

TransactionGetResult getResult = ctx.get(couchbaseClientFactory.getDefaultCollection(), "doc-id");

CouchbaseDocument source = new CouchbaseDocument(getResult.id());
source.setContent(getResult.contentAsObject());
Menu menu = mappingCouchbaseConverter.read(Menu.class, source);

When entity contains a list of nested objects then nested field has incorrect type (ArrayList<HashMap>) and app crashes with ‘java.lang.ClassCastException: class java.util.HashMap cannot be cast to class com.example.MenuCategory’.

MenuCategory category = menu.getCategories().get(0); // ClassCastException

Document in DB:

{
  "_class": "com.example.Menu",
  "categories": [
    {
      "id": "category_1",
      "products": [
        {
          "id": "product_1"
        },
        {
          "id": "product_2"
        }
      ]
    }
  ],
  "name": "Menu 1"
}

com.example.Menu:

@Document
public class Menu {

    public static final String PREFIX = "menu";

    @IdPrefix
    private final String prefix = PREFIX;

    @Id
    @GeneratedValue(strategy = GenerationStrategy.UNIQUE, delimiter = ID_DELIMITER)
    private String id;

    @Field
    private String name;

    @Field
    private List<MenuCategory> categories;

    // getters / setters
}

com.example.MenuCategory:

public class MenuCategory {

    private String id;

    private List<CategoryProduct> products;

    // getters / setters

    public static class CategoryProduct {

        private String id;

        public CategoryProduct() {
        }

        public CategoryProduct(String id) {
            this.id = id;
        }

        // getters / setters
    }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
mikereichecommented, Dec 7, 2021

I’ll keep this open for further investigation as to why the documentation doesn’t match - but the following will work.

Menu m = couchbaseTemplate.support().decodeEntity(getResult.id(), new String(getResult.contentAsBytes()), getResult.cas(), Menu.class);
MenuCategory mc = m.getCategories().get(0);
System.err.println(m);
0reactions
mikereichecommented, Feb 3, 2022

I realize this was first, but please refer to #1312 as it has more information on it. I’ll make this the ‘duplicate’.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Object having id as key-name not getting populated via spring ...
Here user.id is coming as null when fetched from DB. I am using java-sdk-3, spring version 2.3.3.RELEASE, and spring-data-couchbase as 4.0.3.
Read more >
Spring Data Couchbase - Reference Documentation
Kotlin classes are supported to be instantiated, all classes are immutable by default and require explicit property declarations to define mutable properties.
Read more >
Spring Data Couchbase / Spring Boot: could not get nested list ...
I am new to couchbase and I am encountering an issue on fetching document from couchbase. The nested list of object (List of...
Read more >
fix issue with id in nested objects caused by DATACOUCH ...
When a nested document - such as Submission in the User object - contains an 'id', it is also removed from the data....
Read more >
spring-projects/spring-data - Gitter
Hello, I'm using spring-data-couchbase's ReactiveCouchbaseRepository and it seems ... public void setValidator(final Validator validator) { this.validator ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found