2016-06-28 67 views
1

productChanges集合中的文档如下所示。带子文档的Spring mongotemplate查询结果

{ 
    "_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

+0

“我无法获得审核对象填充”:描述发生了什么。任何错误消息?列表简单的时候不应该是空的吗? –

+0

@ antoine-sac没有错误信息。 'productChanges'填充了条目,但'audit'始终为空。 – ravindrab

回答

1

简短的Java类名被映射到以下方式集合名称。类别com.bigbank.SavingsAccount映射到savingsAccount集合名称。

对象的字段用于在 文档中将字段与字段进行转换。公共JavaBean属性不被使用。

由于您的子文档字段命名为Audit和Java字段名是audit,如你预期春天数据不能填充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。并有一个建议,尝试使用一致的命名约定。