MappingException for Map type of data

こ雲淡風輕ζ 提交于 2021-01-03 08:30:03

问题


While saving Map type data in couchBase I am getting an exception

Caused by: org.springframework.data.mapping.MappingException: Couldn't find PersistentEntity for type java.lang.Object!

I've taken a map in DataModel

@Data
public class test { 
  private Map<String,Object> testMap;
 }

I found this and override couchBase configuration to do customMapping in case of Object Type like

protected <R> R read(final TypeInformation<R> type, final CouchbaseDocument source,
        final Object parent) {
      if (Object.class == typeMapper.readType(source, type).getType()) {
        return (R) source.export();
      } else {
        return super.read(type, source, parent);
      }
    } 

It worked for the request like

{  
   "dummyMap":{  
      "key1":"val1",
      "key2":"val2"
   }
}

But failed for

{  
   "dummyMap":{  
      "key1":"val1",
      "key2":"val2",
      "objects":[  
         {  
            "key1":"val1",
            "key2":"val2"
         }
      ]
   }
}

with exception

Caused by: java.lang.IllegalArgumentException: Basic type must not be null!

I guess it is because of the array. Please let me know what I am doing wrong.

I am using spring-data-couchbase version 2.0.4.RELEASE.


回答1:


hi please use below code, its because type is null and couchbase mapping convertor cant read document its must be work.

@Override
    @SuppressWarnings("unchecked")
    protected <R> R read(final TypeInformation<R> type, final CouchbaseDocument source, final Object parent) {
        if (type == null)
            return (R) source.export();
        if (Object.class == typeMapper.readType(source, type).getType()) {
            return (R) source.export();
        } else {
            return super.read(type, source, parent);
        }
    }
    


来源:https://stackoverflow.com/questions/53705146/mappingexception-for-map-type-of-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!