当前位置:网站首页>About MongoDb query Decimal128 to BigDecimal problem

About MongoDb query Decimal128 to BigDecimal problem

2022-08-10 06:32:00 chw-di

Premise: use MongoTemplate to query the database, and the return result type is Map

Decimal128 cannot convert BigDecimal, unless your return type defines the type of the object as BigDecimal, then if you cannot determine what the return value is, you can only use Map to connect to the end of the return.

Solution:

You can use jackson to convert Decimal128 to BigDecimal

Second method I used:

Change the source code.

The getPotentiallyConvertedSimpleRead method in MappingMongoConverter

Checks whether we have a custom conversion for the given simple object. Converts the given value if so, applies {@link Enum} handling or returns the value as is. Can be overridden by subclasses.

Checks whether the given simple object has a custom transformation.If so, convert the given value and apply

 private Object getPotentiallyConvertedSimpleRead(Object value, @Nullable Class target) {if (target==null){return value;}//Convert Decimal128 to BigDecimalif (value.getClass() == Decimal128.class){return ((Decimal128) value).bigDecimalValue();}if (ClassUtils.isAssignableValue(target, value)) {return value;}if (conversions.hasCustomReadTarget(value.getClass(), target)) {return doConvert(value, target);}if (Enum.class.isAssignableFrom(target)) {return Enum.valueOf((Class) target, value.toString());}return doConvert(value, target);}

On the left is the unmodified code, because I used Map to connect the result, so the types are all Object, and there is a misunderstanding before checking whether there is a custom conversion, because ClassUtils was called before.isAssignableValue method

from ClassUtils.java

 public static boolean isAssignableValue(Class type, @Nullable Object value) {Assert.notNull(type, "Type must not be null");return (value != null ? isAssignable(type, value.getClass()) : !type.isPrimitive());}

Determine if the given type is assignable from the given value, assuming setting by reflection.

Determines whether the given type is assignable from the given value, assuming set by reflection.The primitive wrapper class is considered assignable to the corresponding primitive type.

So the code on the right is changed to bigDecimalValue() that takes Decimal128 as long as the value is Decimal128

It is very simple to convert BigDecimal to Decimal128, just write a configuration class and let MongoTemplate load it,

@ReadingConverterpublic class BigDecimalToDecimal128Converter implements Converter {@Overridepublic Decimal128 convert(BigDecimal bigDecimal) {return new Decimal128(bigDecimal);}}
 private static MongoConverter getDefaultMongoConverter(MongoDatabaseFactory factory) {DbRefResolver dbRefResolver = new DefaultDbRefResolver(factory);//Add conversion to BigDecimalToDecimal28List list = new ArrayList<>();list.add(new BigDecimalToDecimal128Converter());MongoCustomConversions conversions = new MongoCustomConversions(list);MongoMappingContext mappingContext = new MongoMappingContext();mappingContext.setSimpleTypeHolder(conversions.getSimpleTypeHolder());mappingContext.afterPropertiesSet();MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mappingContext);converter.setCustomConversions(conversions);converter.setCodecRegistryProvider(factory);converter.afterPropertiesSet();return converter;}

Database:

Return result:

原网站

版权声明
本文为[chw-di]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/222/202208100546455637.html