Reading BigDecimal with Morphia

廉价感情. 提交于 2019-12-25 03:57:11

问题


I have a POJO that uses BigDecimal. When I save the object to morphia, it is clear the value is saved as a string. But if I modify the database from the javascript shell to have some decimal number value, The try to read the object using the morphia class, It fails with the following error:

For example:

@Entity(value = "table_name", noClassnameStored = true)
public class Advertisement implements Table {
  BigDecimal value;
}

java.lang.ArrayStoreException
    at java.lang.System.arraycopy(Native Method)
    at java.util.ArrayList.toArray(ArrayList.java:361)
    at org.mongodb.morphia.utils.ReflectionUtils.convertToArray(ReflectionUtils.java:537)
    at org.mongodb.morphia.converters.IntegerConverter.decode(IntegerConverter.java:35)
    at org.mongodb.morphia.converters.DefaultConverters.fromDBObject(DefaultConverters.java:134)
    at org.mongodb.morphia.mapping.ValueMapper.fromDBObject(ValueMapper.java:27)
    at org.mongodb.morphia.mapping.Mapper.readMappedField(Mapper.java:604)
    at org.mongodb.morphia.mapping.Mapper.fromDb(Mapper.java:585)
    at org.mongodb.morphia.mapping.Mapper.fromDBObject(Mapper.java:296)
    at org.mongodb.morphia.query.MorphiaIterator.convertItem(MorphiaIterator.java:78)
    at org.mongodb.morphia.query.MorphiaIterator.processItem(MorphiaIterator.java:65)
    at org.mongodb.morphia.query.MorphiaIterator.next(MorphiaIterator.java:60)
    at org.mongodb.morphia.query.QueryImpl.asList(QueryImpl.java:294)
    at 
...

What is the proper form to add a decimal value in string form to mongodb such that morphia can read that value into a Java BigDecimal ?


回答1:


As @evanchooly references, I already had the BigDecimal code to be able to use that that of object in Morphia. This is the code:

public class BigDecimalConverter extends TypeConverter {

public BigDecimalConverter() {
    super(BigDecimal.class);
}

@Override
public Object encode(Object value, MappedField optionalExtraInfo) {
    BigDecimal val = (BigDecimal) value;
    if (val == null)
        return null;
    return val.toPlainString();
}

@Override
public Object decode(Class targetClass, Object fromDBObject,
        MappedField optionalExtraInfo) {
    if (fromDBObject == null)
        return null;
    BigDecimal dec = new BigDecimal(fromDBObject.toString());
    return dec;
}

}

This was working until recently when I created a new collection POJO.

Weirdly enough, the work-around was to use @Property() annotation for the variable. Instead of allowing morphia to name the key.




回答2:


You can see a discussion of this very issue on the mailing list with a few suggested solutions.



来源:https://stackoverflow.com/questions/24498011/reading-bigdecimal-with-morphia

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