gpt4 book ai didi

java - Spring mongotemplate 查询结果与子文档

转载 作者:行者123 更新时间:2023-11-30 07:17:50 24 4
gpt4 key购买 nike

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

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 Data 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 Data 无法填充 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 。一句建议是,尝试使用一致的命名约定。

关于java - Spring mongotemplate 查询结果与子文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38080472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com