解释这个 JPA 查询代码在做什么

问题描述 投票:0回答:1

我收到这样的查询,无法理解这个查询在做什么!!

    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 jpa spring-data-jpa criteria-api
1个回答
0
投票

这是 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。

© www.soinside.com 2019 - 2024. All rights reserved.