2016-03-31 25 views
0

由於在批量寫入期間觀察到的問題,我正在嘗試升級我項目的Morphia jar。morphia MappingException:來自轉換器(IdentityConverter)的錯誤設置值

從Morphia 0.99到1.1.1。更新後,我在閱讀實現Iterable的嵌入式類對象時遇到了問題。

以下是我在閱讀了該錯誤,堅持記錄:

了java.lang.RuntimeException: org.mongodb.morphia.mapping.MappingException:從 轉換錯誤設定值(IdentityConverter)

我正在使用Mongo 2.7.3,目前無法進行升級。

編輯:全堆棧跟蹤:

Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: org.mongodb.morphia.mapping.MappingException: Error setting value from converter (IdentityConverter) for Doc.expressionTraversalTree to { "levels" : [ { "computeUnits" : [ { "computeType" : "MATH_EVAL" , "mvalExpression" : "doc['__count__'].value + 10" }] , "level" : 0}]} 
    at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:74) 
    at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:772) 
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:230) 
    at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:191) 
    at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:134) 
    at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:146) 
    at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:117) 
    at org.mongodb.morphia.query.QueryImpl.get(QueryImpl.java:220) 
    at org.mongodb.morphia.dao.BasicDAO.findOne(BasicDAO.java:188) 
    . 
    . 
    . 
Caused by: java.lang.RuntimeException: org.mongodb.morphia.mapping.MappingException: Error setting value from converter (IdentityConverter) for Doc.expressionTraversalTree to { "levels" : [ { "computeUnits" : [ { "computeType" : "MATH_EVAL" , "mvalExpression" : "doc['__count__'].value + 10" }] , "level" : 0}]} 
    at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:74) 
    at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:768) 
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:230) 
    at org.mongodb.morphia.mapping.EmbeddedMapper.readMapOrCollectionOrEntity(EmbeddedMapper.java:206) 
    at org.mongodb.morphia.mapping.EmbeddedMapper.readCollection(EmbeddedMapper.java:142) 
    at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:45) 
    ... 14 more 
Caused by: org.mongodb.morphia.mapping.MappingException: Error setting value from converter (IdentityConverter) for Doc.expressionTraversalTree to { "levels" : [ { "computeUnits" : [ { "computeType" : "MATH_EVAL" , "mvalExpression" : "doc['__count__'].value + 10" , "computeLevel" : 0 , "applyFilter" : false }] , "level" : 0}]} 
    at org.mongodb.morphia.converters.Converters.fromDBObject(Converters.java:128) 
    at org.mongodb.morphia.mapping.EmbeddedMapper.fromDBObject(EmbeddedMapper.java:56) 
    ... 19 more 
Caused by: java.lang.IllegalArgumentException: Can not set ComputeExpTraversalTree field Doc.expressionTraversalTree to com.mongodb.BasicDBObject 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164) 
    at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168) 
    at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) 
    at java.lang.reflect.Field.set(Field.java:741) 
    at org.mongodb.morphia.mapping.MappedField.setFieldValue(MappedField.java:410) 
    at org.mongodb.morphia.converters.Converters.fromDBObject(Converters.java:126) 
    ... 20 more 
+0

這將有助於看到實際的堆棧跟蹤。通常有一個「引起」部分,有助於看到。 – evanchooly

+0

@evanchooly我已經添加了堆棧跟蹤。請確認。 –

回答

0

固定的問題。作爲答案發布,因爲它可能有助於某人。

根本原因是對象字段'ComputeExpTraversalTree',Morphia無法將此對象轉換爲Mongo的BasicDBObject。

我已經添加了一個自定義的轉換器,它將從BasicDBObject對&進行編碼和解碼。

轉換器片段以及如何在Morphia客戶端上進行配置。

Converter類:

public class ComputeExpressionTreeConverter extends TypeConverter implements SimpleValueConverter { 

    public ComputeExpressionTreeConverter() { 
     super(ComputeExpTraversalTree.class); 
    } 

    @Override 
    public Object encode(Object value, MappedField optionalExtraInfo) { 
     if (value == null) { 
      return null; 
     } 
     DBObject dbo = null; 
     String expressionTree=new Gson().toJson(
       value, ComputeExpTraversalTree.class); 
     dbo=BasicDBObject.parse(expressionTree); 
     return dbo; 
    } 

    @Override 
    @SuppressWarnings("rawtypes") 
    public Object decode(Class targetClass, Object fromDBObject, MappedField field) throws MappingException { 
     if (fromDBObject == null) { 
      return null; 
     } 
     DBObject dbo = (DBObject) fromDBObject; 
     JsonElement jsonElement = new JsonParser().parse(dbo.toString()); 
     ComputeExpTraversalTree expTraversalTree = new Gson().fromJson(
       jsonElement, ComputeExpTraversalTree.class); 
     return expTraversalTree; 
    } 

} 

配置:

Morphia morphia = new Morphia(); 
morphia.getMapper().getConverters().addConverter(cls); 

希望這會有所幫助。

相關問題