【问题标题】:why Spring Data JPA documentation describes query methods starting only with find?为什么 Spring Data JPA 文档描述了仅以 find 开头的查询方法?
【发布时间】:2018-08-24 12:10:17
【问题描述】:

Reference documentation of Spring Data JPA framework通过示例描述查询方法。这种方法的例子是findByEmailAddressAndLastname。它以find 开头。然后他们在方法名称中列出所有支持的关键字。我发现命名查询必须以find开头。

但是,以下方法按设计工作,尽管它以 exists 开头

@Repository
public interface UserRepository extends CrudRepository<User, Integer> {
    boolean existsUserByEmail(String email);
}

它运行一个命令

select user0_.id as col_0_0_ from users user0_ where user0_.email=? limit ?

并检查用户是否存在。但是创建该方法时,我依赖于 IDE,而不是文档。 Spring Data JPA 文档或其他地方(可能是Query DSL reference)中是否有任何声明可用于创建此类方法?

【问题讨论】:

  • 大部分示例似乎使用find,这是可以理解的,因为它给出了一致的命名。在some examples(示例9)中,您看到find/get,但似乎可以随意选择第一部分。不知道记录在哪里(以及记录得如何),但它可能在某个地方。

标签: java spring spring-data-jpa code-documentation


【解决方案1】:

从 Spring in action 第 4 版开始,不仅支持“查找”,它指出 “正如你所看到的,动词是被读取的,而不是前面例子中的 find。 Spring Data 允许方法名中有四个动词:get、read、find 和 count。这 get、read 和 find 动词是同义词” 关键字 EXISTS 也在这里提到 https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#new-features.1-11-0 作为一个新功能。

【讨论】:

  • 我一定是个盲人。我看不到你引用的句子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-29
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2019-07-11
  • 2021-03-16
  • 1970-01-01
相关资源
最近更新 更多