【发布时间】:2020-05-23 19:50:33
【问题描述】:
我正在使用 Spring (boot) 2.2.7 和在 Debian Stretch (9.12) 上运行的 mongoDB 4.0。 我已经设置了 3 个我试图通过聚合查找级联操作加入的集合。
- 操作(节点类)
- 库存(组件类)
- 目录(产品类)
每个类都有一个将它们链接在一起的 uuid 字符串属性。
节点(传感器、接收器、基站等)
@Document(collection = "operations")
public class Node implements Serializable {
@Id
private String _id;
private String componentUuId;
....
}
组件(带有序列号的交付产品,裸码...)
@Document(collection = "stock")
public class Component implements Serializable {
@Id
private String _id;
private String uuId;
private String productUuId;
....
}
产品(市场人工制品)
@Document(collection = "catalog")
public class Product implements Serializable {
@Id
private String _id;
private String uuId;
....
}
在 mongo shell 中运行此命令时:
db.operations.aggregate([
{
$lookup: {
"from": "stock",
"localField": "componentUuId",
"foreignField": "uuId",
"as": "component"
}
},
{
$lookup: {
"from": "catalog",
"localField": "component.productUuId",
"foreignField": "uuId",
"as": "product"
}
}
]).pretty()
我明白了
{
"_id" : ObjectId("5ec952a7714e7e248b5fb98b"),
"uuId" : "3b5e3417-166c-4d69-9fa5-b0856d819541",
"componentUuId" : "57eff5c5-8b3c-43cb-9f2d-1c4098fc0c1f",
"component" : [
{
"_id" : ObjectId("5ec952a4714e7e248b5fb97f"),
"uuId" : "57eff5c5-8b3c-43cb-9f2d-1c4098fc0c1f",
"productUuId" : "3da7a6a6-ccf6-4fe1-892a-a6d6b82396c1",
"created" : ISODate("2020-05-23T14:43:16.588Z"),
"_class" : "org.open_si.udc_common.models.Component"
.......
}
],
"product" : [
{
"_id" : ObjectId("5ec952a1714e7e248b5fb969"),
"uuId" : "3da7a6a6-ccf6-4fe1-892a-a6d6b82396c1",
"model" : "TMP36GT9Z",
"_class" : "org.open_si.udc_common.models.Product"
......
}
]
}
为了在 Java 中实现相同的目标(使用 Spring-data-mongodb),我跑了
Aggregation agg = Aggregation.newAggregation(
Aggregation.lookup(mongoTemplate.getCollectionName(Component.class),
"componentUuId", "uuId", "component"),
Aggregation.lookup(mongoTemplate.getCollectionName(Product.class),
"component.productUuId", "uuId", "product")
);
AggregationResults<NodeExpanded> results = mongoTemplate.aggregate(agg, mongoTemplate.getCollectionName(Node.class), NodeExpanded.class);
return results.getMappedResults();
使用 NodeExpanded 类
public class NodeExpanded implements Serializable {
private String uuId;
private Component component;
private Product product;
....
}
注意:我没有扩展 Node 基类,因为在出现以下问题之前出现重复键错误。
问题是我在托管该代码的服务中遇到错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException: null
不幸的是,我不知道如何在 Spring 的较低级别进行调试,也无法找出原始错误是什么。
请注意,获取链接到 Component 的 Product 的代码可以正常工作:
Aggregation agg = Aggregation.newAggregation(
Aggregation.lookup(mongoTemplate.getCollectionName(Product.class),
"productUuId", "uuId", "product")
);
AggregationResults<ComponentExpanded> results =
mongoTemplate.aggregate(agg,mongoTemplate.getCollectionName(Component.class), ComponentExpanded.class);
return results.getMappedResults();
使用扩展类:
public class ComponentExpanded implements Serializable {
private String uuId;
private Product product;
...
}
有什么想法吗?提前致谢。
编辑
为了更深入地调试,我将代码限制为仅将 operations 集合(Node.class)加入到 stock 中的一个(Component.class)我对 stock (Component.class) 和 catalog (Product.class) 的处理方式相同。
public List<NodeExpanded> list() {
Aggregation agg = Aggregation.newAggregation(
Aggregation.lookup(mongoTemplate.getCollectionName(Component.class),
"componentUuId", "uuId", "component")
);
AggregationResults<NodeExpanded> results = mongoTemplate.aggregate(agg, mongoTemplate.getCollectionName(Node.class), NodeExpanded.class);
return results.getMappedResults();
}
用一个简单的 NodeExpanded 类作为
public class NodeExpanded implements Serializable {
private String uuId;
private Component component;
}
我在 mongoTemplate.aggregate() 指令上得到相同的空指针异常
已解决
问题来自没有 @Autowired MongoTemplate 的 SensorPersistenceService。 根据该帖子中提供的信息,无法找出错误原因。 再次感谢@Valijon 在此任务中提供的帮助^^
【问题讨论】:
-
看来你初始化错误
SensorPersistenceService。尝试将以..Service、Repository和@Autowired结尾的属性标注。 -
去试试。我不自动装配服务,因为它们被注入到控制器构造器中。只有存储库是。
-
你是对的,不是关于应用程序服务/存储库,而是关于在 SensorPersistenceService 中不是 @Autowired 的 MongoTemplate。
标签: mongodb spring-boot aggregation-framework cascade lookup-tables