春蒙戈条件的复杂查询匹配查询实体
问题描述:
的内部对象场,我有两个实体春蒙戈条件的复杂查询匹配查询实体
class EntityA{
@Id
String id;
..... other class fields
..... getter/setters
}
class EntityB{
@Id
String id;
Integer age;
EntityA entityA;
..... other class fields
..... getter/setters
}
,我由id of entityA
对象匹配查询EntityB。
这里是我想要做的
Criteria criteria = Criteria.where("age").gte(0).lt(100);
EntityA entityA = new EntityA();
entityA.setId("theIdIHave");
Example<EntityA> eaEx = Example.of(entityA);
criteria = criteria.andOperator(Criteria.byExample(eaEx));
Query query = new Query();
query.addCriteria(criteria);
List<EntityB> res = mongoTemplate.find(query,EntityB.class);
但它抛出
java.lang.RuntimeException: json can't serialize type : class org.springframework.data.domain.Example
两件事情 是我使用org.springframework.data.domain.Example的做法是正确的? ? 以及为什么是错误
答
请在下面尝试。它将反序列化结果并映射到EntityB pojo
类
Query query = new Query();
query.addCriteria(Criteria.where("age").gte(0).lt(100).and("entityA.id").is("theIdIHave");
List<EntityB> res = mongoTemplate.find(query, EntityB.class);
请提供示例文档 –