我收到这样的查询,无法理解这个查询在做什么!!
SimpleJpaRepository<SomeEntity, Long> repo = this.repoFactory.createJpaRepository(SomeEntity.class, Boolean.FALSE);
Optional<SomeEntity> someKeyOpt = repo.findOne((root, query, cb) -> {
return cb.and(cb.equal(root.get("active"), Boolean.TRUE));
});
if (someKeyOpt.isPresent()) {
return someKeyOpt.get().getPublicKey();
}
return null;
这个 lambda 表达式在做什么?为什么我们甚至在这里拥有它?这个根是什么?这是什么 cb 东西?
(root, query, cb) -> {
return cb.and(cb.equal(root.get("active"), Boolean.TRUE));
}
这是 Spring Data JPA Criteria Query.
的示例this.repoFactory.createJpaRepository(SomeEntity.class, Boolean.FALSE);
此行调用 repoFactory,一些自定义工厂实现,并在其上调用 createJpaRepository。 注意它,传递 SomeEntity.class,这有助于在 SomeEntity.class 上创建一个存储库,而且我们传递 Boolean.FALSE,这似乎是禁用缓存或一些此类相关参数值。
现在让我们来看看Lambda,
root : In Criteria Query, the specifies the Entity, from which the data will be selected.
cb : It's the criteria builder here.
所以查询实际上返回的是 SomeEntity 表中那些在行中具有活动值的行设置为 True。
得到它后,它返回公钥。 如果不是,则返回 null。