我们的应用程序使用 Spring boot 3.3.6 并具有以下场景:
型号:
@Document(collection = "STORAGE_CLASS")
public class StorageClass {
@Field("STATUS")
private String status;
@Field("FIELD_NUMBER")
private long numberField;
... // getters, setters, tostring
}
我们需要每 10 秒在数据库中查询一次这个集合,所以我们为它编写了一个 dao:
public interface StorageClassDao {
List<StorageClass> getStorage();
}
@Repository
public class StorageClassDaoImpl implements StorageClassDao {
private MongoTemplate mongoTemplate;
public StorageClassDaoImpl(MongoTemplate mongoTemplate) {
this.mongoTemplate = mongoTemplate;
}
@Override
public List<StorageClass> getStorage() {
Criteria criteria = Criteria.where("STATUS").is("E")
.and("FIELD_NUMBER").is(1);
Query query = new Query(criteria);
return this.mongoTemplate.find(query, StorageClass.class);
}
}
这有效,我从 getStorage 接收与我传递的查询匹配的数据。但是,我看到抛出了很多 PropertyReferenceException(如果我为此异常添加断点),消息如下:
No property 'STATUS' found for type 'StorageClass'
No property 'STATU' found for type 'StorageClass'
No property 'STAT' found for type 'StorageClass'
No property 'STA' found for type 'StorageClass'
No property 'ST' found for type 'StorageClass'
No property 's' found for type 'StorageClass'
No property 'FIELD' found for type 'StorageClass'
No property 'FIEL' found for type 'StorageClass'
No property 'FIE' found for type 'StorageClass'
No property 'FI' found for type 'StorageClass'
No property 'f' found for type 'StorageClass'
No property 'FIELD_NUMBER' found for type 'StorageClass'
No property 'FIELD_NUMBE' found for type 'StorageClass'
No property 'FIELD_NUMB' found for type 'StorageClass
No property 'FIELD_NUM' found for type 'StorageClass'
No property 'FIELD_NU' found for type 'StorageClass'
No property 'FIELD_N' found for type 'StorageClass''
No property 'FIELD_' found for type 'StorageClass'
异常从两个地方抛出:
Exception 'org.springframework.data.mapping.PropertyReferenceException' occurred in thread 'scheduling-1' at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:490)
Exception 'org.springframework.data.mapping.PropertyReferenceException' occurred in thread 'scheduling-1' at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:94)
对于每个服务调用,都会抛出 100 多个此类异常。
我在这里做错了什么?
参考代码:https://github.com/felipeissa/mongo-db-exception
Spring启动版本:3.3.6 Spring数据mongodb版本:4.3.6 Java版本:21
问题在于您如何命名字段
STATUS, FIELD_NUMBER
以及您如何尝试使用它们。正确的使用方法是使用变量名称 status , numberField
:
更新代码:
@Override
public List<StorageClass> getStorage() {
Criteria criteria = Criteria.where("status").is("E")
.and("numberField").is(1);
Query query = new Query(criteria);
return this.mongoTemplate.find(query, StorageClass.class);
}