[英]JPA/JPQL automatic method query generation
I have a co-worker that just blew my mind.我有一个同事让我大吃一惊。 We have a Java 11/Spring Boot/Hibernate/JPA app talking to a MySQL DB.
我们有一个 Java 11/Spring Boot/Hibernate/JPA 应用程序与 MySQL 数据库通信。 Apparently JPA JPQL (or something similar to that) is capable of -- but only if you write the repository methods correctly -- building out queries based on your method name.
显然,JPA JPQL(或类似的东西)能够 - 但前提是您正确编写存储库方法 - 根据您的方法名称构建查询。
So for instance if we have a JPA entity:例如,如果我们有一个 JPA 实体:
@Entity
@Table(name = "accounts")
@Data
public class Account {
@Column(name = "account_email")
private String email;
// ... many more fields down here
}
And then a repository for it:然后是它的存储库:
@Repository
public interface AccountRepository extends JpaRepository<Account,Long> {
@Query("FROM Account WHERE email = :email")
Account findByEmail(@Param(value = "email") String email);
}
Apparently (and this might be a bad example) I could just simplify that to:显然(这可能是一个不好的例子)我可以将其简化为:
@Repository
public interface AccountRepository extends JpaRepository<Account,Long> {
Account findByEmail(String email);
}
And JPA/JPQL will figure out that since I want to " findByEmail
" and Account#email
exists, it just wants me to do a SELECT * FROM accounts where email = ?
JPA/JPQL 会发现,既然我想“
findByEmail
”并且Account#email
存在,它只需要我做一个SELECT * FROM accounts where email = ?
. . Amazing!
惊人!
The only problem is: I don't see this documented anywhere well , and I don't see it documented anywhere officially .唯一的问题是:我没有看到它在任何地方都有很好的记录,我也没有看到它在任何地方正式记录。 There's a few old blogs that I was able to find that insinuate the same things, but nowhere official (JPA docs, JPQL docs, etc.) that go into detail as to how it works and what its limitations are.
我可以找到一些旧博客暗示相同的事情,但没有官方(JPA 文档、JPQL 文档等)详细介绍它的工作原理和局限性。
Can anyone point me in the right direction?谁能指出我正确的方向? What is this mysterious feature/technology called and what are its limitations/capabilities?
这种神秘的特性/技术叫什么,它的局限性/能力是什么? Can it only work on SELECTs or can it handle inserts/updates/deletes as well?
它只能在 SELECT 上工作还是也可以处理插入/更新/删除?
This is part of the Spring Data support for JPA.这是 Spring Data 对 JPA 的支持的一部分。 You can find more info at the documentation Query Methods and all the supported query-keywords in the appendix section.
您可以在文档查询方法和附录部分中的所有支持的查询关键字中找到更多信息。 Here is an excerpt from the documentation:
这是文档的摘录:
Keyword![]() |
Description![]() |
---|---|
find…By, read…By, get…By, query…By, search…By, stream…By![]() |
General query method returning typically the repository type, a Collection or Streamable subtype or a result wrapper such as Page, GeoResults or any other store-specific result wrapper.![]() ![]() |
exists…By![]() |
Exists projection, returning typically a boolean result.![]() |
count…By![]() |
Count projection returning a numeric result.![]() |
delete…By, remove…By![]() |
Delete query method returning either no result (void) or the delete count.![]() |
…First…, …Top… ![]() |
Limit the query results to the first of results.![]() ![]() |
…Distinct… ![]() |
Use a distinct query to return only unique results.![]() ![]() ![]() |
Logical keyword![]() |
Keyword expressions![]() |
---|---|
AND![]() |
And![]() |
OR![]() |
Or![]() |
AFTER![]() |
After, IsAfter![]() |
BEFORE![]() |
Before, IsBefore![]() |
CONTAINING![]() |
Containing, IsContaining, Contains![]() |
BETWEEN![]() |
Between, IsBetween![]() |
ENDING_WITH ![]() |
EndingWith, IsEndingWith, EndsWith ![]() |
EXISTS![]() |
Exists![]() |
FALSE![]() |
False, IsFalse![]() |
GREATER_THAN![]() |
GreaterThan, IsGreaterThan![]() |
GREATER_THAN_EQUALS ![]() |
GreaterThanEqual, IsGreaterThanEqual![]() |
IN![]() |
In, IsIn![]() |
IS![]() |
Is, Equals, (or no keyword)![]() |
IS_EMPTY![]() |
IsEmpty, Empty![]() |
IS_NOT_EMPTY ![]() |
IsNotEmpty, NotEmpty ![]() |
IS_NOT_NULL ![]() |
NotNull, IsNotNull ![]() |
IS_NULL![]() |
Null, IsNull![]() |
LESS_THAN![]() |
LessThan, IsLessThan![]() |
LESS_THAN_EQUAL ![]() |
LessThanEqual, IsLessThanEqual![]() |
LIKE![]() |
Like, IsLike![]() |
NEAR![]() |
Near, IsNear![]() |
NOT![]() |
Not, IsNot![]() |
NOT_IN ![]() |
NotIn, IsNotIn![]() |
NOT_LIKE![]() |
NotLike, IsNotLike![]() |
REGEX![]() |
Regex, MatchesRegex, Matches![]() |
STARTING_WITH![]() |
StartingWith, IsStartingWith, StartsWith![]() |
TRUE![]() |
True, IsTrue![]() |
WITHIN![]() |
Within, IsWithin![]() |
Not much of a mystery and quite well documented to be perfectly honest.没什么神秘的,而且有据可查,完全诚实。 What you mention is called a derived query which basically infers the JPQL query from the method name.
您提到的称为派生查询,它基本上从方法名称中推断出 JPQL 查询。 A simple Google search can turn up quite some documentation of this feature.
一个简单的谷歌搜索可以找到相当多的这个特性的文档。 Also the Spring Data JPA documentation clearly documents this and well as its usage.
Spring Data JPA 文档也清楚地记录了这一点及其用法。 Check here for more.
在这里查看更多信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.