在尝试将QueryDSL实现应用于搜索过滤时,我遇到了以下问题,至少可以这样说。
请在名为付款的MongoDB集合中考虑以下属性:
对象1- amountDue (Object) - amount => 106.00 (String)
对象2- amountDue (Object) - amount => 58.80 (String)
这些值是由系统生成的,amountDue
对象的实际数据类型是org.joda.BigMoney对象。
这些属性以绑定方法应用,该方法用于提供QueryDSL谓词,以便返回amountDue.amount
属性大于搜索查询中指定的所有Payment对象。这种方法描述如下:
@Override
public Predicate bind(NumberPath<BigDecimal> bigDecimalNumberPath, Collection<? extends BigDecimal> value) {
ArrayList<? extends BigDecimal> amounts = new ArrayList<>(value);
return bigDecimalNumberPath.gt(amounts.get(0));
}
以下内容描述了我正在测试的案例以及相应的结果:
{URL}/payments/filter?page=0&amountDue.amount=10.00
,在内部转换为'amountDue.amount> 10.00'谓词将返回两个对象[正确]{URL}/payments/filter?page=0&amountDue.amount=20.00
,在内部转换为'amountDue.amount> 20.00'谓词,仅返回对象2 [不正确]{URL}/payments/filter?page=0&amountDue.amount=60.00
,内部转换为'amountDue.amount> 60.00'谓词的对象不返回任何对象[不正确]{URL}/payments/filter?page=0&amountDue.amount=100.00
,在内部转换为'amountDue.amount> 100.00'谓词,仅返回对象2 [不正确]{URL}/payments/filter?page=0&amountDue.amount=150.00
,在内部转换为'amountDue.amount> 150.00'谓词,仅返回对象2 [不正确]当对象1的
amount
值更改为小于100的值时,所有情况将返回正确的结果。请问您有什么建议/建议?
谢谢你的时间!!
最佳答案
已应用以下方法解决上述问题:
首先创建一个Decimal128(Bson类型)到Big Decimal类的转换器:
public class Decimal128ToBigDecimalConverter implements Converter<Decimal128, BigDecimal> {
@Override
public BigDecimal convert(Decimal128 source) {
return source.bigDecimalValue();
}
}
然后创建一个从Big Decimal到Decimal128(Bson类型)的类转换器:
public class BigDecimalToDecimal128Converter implements Converter<BigDecimal, Decimal128> {
@Override
public Decimal128 convert(BigDecimal source) {
return new Decimal128(source);
}
}
最后,配置您的MongoConfig文件以使用转换器:
@Bean
public MongoTemplate mongoTemplate() throws Exception {
MongoTemplate mongoTemplate = new MongoTemplate(mongo(), getDatabaseName());
MappingMongoConverter mongoMapping = (MappingMongoConverter) mongoTemplate.getConverter();
mongoMapping.setCustomConversions(customConversions());
mongoMapping.afterPropertiesSet();
return mongoTemplate;
}
public CustomConversions customConversions() {
return new CustomConversions(Arrays.asList(new Decimal128ToBigDecimalConverter(), new BigDecimalToDecimal128Converter()));
}
/* (non-Javadoc)
* @see org.springframework.data.mongodb.config.AbstractMongoConfiguration#mongo()
*/
@Bean
@Override
public Mongo mongo() throws Exception
{
return new MongoClient();
}
通过遵循此处列出的示例来实施该解决方案:http://ufasoli.blogspot.com.mt/2017/06/custom-converter-for-mongodb-and-spring.html
关于java - QueryDSL NumberPath <BigDecimal>大于MongoDB集合中给出的结果不准确,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50306664/