Spring 带有子文档的 mongotemplate 查询结果
Spring mongotemplate query result with subDocuments
productChanges
集合中的一个文档如下所示。
{
"_id" : NumberLong(9780876590034),
"isbn" : NumberLong(9780876590034),
"updDtime" : ISODate("2016-06-08T14:02:29.044Z"),
"Audit" : {
"LastProcCntrlNo" : 100192211,
"UpdDtime" : ISODate("2016-06-08T14:02:29.044Z"),
"AddDtime" : ISODate("2016-06-08T14:02:29.044Z")
}
}
我有我的ProductChanges.java
class
public class ProductChanges {
Long isbn;
Date updDtime;
Audit audit;
// getters & setters
}
我使用 mongoTemplate
查询数据库,但无法填充 Audit
对象。
// query the DB
List<ProductChanges> productChanges = mongoTemplate.find(query, ProductChanges.class, "productChanges");
这应该很简单。我需要注释我的 Audit
对象吗?我是不是遗漏了一些微不足道的东西?
Spring 数据 MongoDB 文档对找到此问题的答案没有帮助。
基于Spring Data MongoDB documentation:
The short Java class name is mapped to the collection name in the following manner. The class com.bigbank.SavingsAccount
maps to savingsAccount
collection name.
The fields of an object are used to convert to and from fields in the
document. Public JavaBean properties are not used.
由于您的子文档字段名为 Audit
且 Java 字段名称为 audit
,Spring 数据无法填充 audit
字段如您所料。
为了解决这个问题,您应该将您的字段重命名为 Audit
:
public class ProductChanges {
Long isbn;
Date updDtime;
Audit Audit; // renamed to Audit from audit
// getters & setters
}
或使用@Field
注释:
public class ProductChanges {
Long isbn;
Date updDtime;
@Field("Audit") Audit audit;
// getters & setters
}
您可以阅读有关映射注释的更多信息here。还有一个忠告,尽量使用一致的命名约定。
productChanges
集合中的一个文档如下所示。
{
"_id" : NumberLong(9780876590034),
"isbn" : NumberLong(9780876590034),
"updDtime" : ISODate("2016-06-08T14:02:29.044Z"),
"Audit" : {
"LastProcCntrlNo" : 100192211,
"UpdDtime" : ISODate("2016-06-08T14:02:29.044Z"),
"AddDtime" : ISODate("2016-06-08T14:02:29.044Z")
}
}
我有我的ProductChanges.java
class
public class ProductChanges {
Long isbn;
Date updDtime;
Audit audit;
// getters & setters
}
我使用 mongoTemplate
查询数据库,但无法填充 Audit
对象。
// query the DB
List<ProductChanges> productChanges = mongoTemplate.find(query, ProductChanges.class, "productChanges");
这应该很简单。我需要注释我的 Audit
对象吗?我是不是遗漏了一些微不足道的东西?
Spring 数据 MongoDB 文档对找到此问题的答案没有帮助。
基于Spring Data MongoDB documentation:
The short Java class name is mapped to the collection name in the following manner. The class
com.bigbank.SavingsAccount
maps tosavingsAccount
collection name.The fields of an object are used to convert to and from fields in the document. Public JavaBean properties are not used.
由于您的子文档字段名为 Audit
且 Java 字段名称为 audit
,Spring 数据无法填充 audit
字段如您所料。
为了解决这个问题,您应该将您的字段重命名为 Audit
:
public class ProductChanges {
Long isbn;
Date updDtime;
Audit Audit; // renamed to Audit from audit
// getters & setters
}
或使用@Field
注释:
public class ProductChanges {
Long isbn;
Date updDtime;
@Field("Audit") Audit audit;
// getters & setters
}
您可以阅读有关映射注释的更多信息here。还有一个忠告,尽量使用一致的命名约定。