要查詢的mongo數據:
{
"_id" : ***,
"_class" : "***",
"downloadCount" : {
"china" : 20,
"google" : 10
}
}
{
"_id" : ***,
"_class" : "***",
"downloadCount" : {
"china" : 10,
"google" : 5
}
}
{
"_id" : ***,
"_class" : "***",
"downloadCount" : {
"china" : 15,
"google" : 2
}
}
查詢:
Query query = new Query();
query.with(new Sort(Direction.DESC, "downloadCount.china"));
mongoTemplate.find(query, this.getEntityClass());
錯誤信息:
java.lang.IllegalAccessError: org/springframework/beans/PropertyMatches
at org.springframework.data.mapping.PropertyReferenceException.detectPotentialMatches(PropertyReferenceException.java:134)
at org.springframework.data.mapping.PropertyReferenceException.<init>(PropertyReferenceException.java:59)
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:75)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:291)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:273)
at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.getPath(QueryMapper.java:837)
at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.<init>(QueryMapper.java:729)
查詢子文檔也是不可以,報相同的錯誤!!!!
query.addCriteria(Criteria.where("downloadCount.china").is(10));
只有單層的查詢和排序是可以正常的,但是我用mongo語句就是可以正常查詢和排序的。
跪求大神幫助!!!
解決了,原因是我的實體類中downloadCount是用JSONObject表示的,但我的查詢是用的downloadCount.china,這樣就導致spring找不到映射字段china,從而報錯。
修改方法:將downloadCount寫成內部類,china和google是其中的字段,如此就可以使用downloadCount.china查詢或者排序了。
總結:spring-data-mongo 太TMD的不是東西了,就不能好好地對原生支持麼XXX!