Reading BigDecimal with Morphia

2019-09-06 00:06发布

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 ?

2条回答
我命由我不由天
2楼-- · 2019-09-06 00:54

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

查看更多
一纸荒年 Trace。
3楼-- · 2019-09-06 01:05

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.

查看更多
登录 后发表回答