如何缩短Spring Data JPA存储库中查询方法的名称?

时间:2022-09-11 16:27:01

Consider a Spring Data Jpa Repository:

考虑一下Spring Data Jpa存储库:

public interface UserRepository extends JpaRepository<User, Long> {

    User findOneByDeletedIsFalseAndActivationKey(String activationKey);

    List<User> findAllByDeletedIsFalseAndActivatedIsFalseAndCreatedDateBefore(DateTime dateTime);

    User findOneByDeletedIsFalseAndLogin(String login);

    User findOneByDeletedIsFalseAndEmail(String email);

}

Notice each method has "DeletedIsFalse" in it. Is there a simple way to make method names shorter? Like i.e.:

请注意,每个方法中都包含“DeletedIsFalse”。有一种简单的方法可以缩短方法名称吗?像:

@FullMethodName("findOneByDeletedIsFalseAndEmail")
User findOneByEmail(String email);

2 个解决方案

#1


Use default Java 8 feature for wrapping, just like so:

使用默认的Java 8功能进行换行,如下所示:

// use findOneByEmail instead
User findOneByDeletedIsFalseAndEmail(String email);

default User findOneByEmail(String email) {
    return findOneByDeletedIsFalseAndEmail(email);
}

See an example.

看一个例子。

#2


Now you can use Java 8 default interface methods as @Maksim Kostromin described. But there is no such a feature in Spring.

现在您可以使用Java 8默认接口方法,如@Maksim Kostromin所述。但是春天没有这样的功能。

-- Old answer

- 老答案

There is no such a way. You can specify any name for a method and add an annotation @Query with parameter value which holds desired query to database like this:

没有这样的方法。您可以为方法指定任何名称,并使用参数值添加注释@Query,该参数值将所需的查询保存到数据库,如下所示:

@Query(value="select u from User u where u.deleted=false and u.email=:email")
User findOneByEmail(@Param("email")String email);

or, with native sql query:

或者,使用本机SQL查询:

@Query(value="SELECT * FROM users WHERE deleted=false AND email=?1", nativeQuery=true)
User findOneByEmail(String email);

You can also use names that follow the naming convention for queries since @Query annotation will take precedence over query from method name.

您还可以使用遵循命名约定的名称进行查询,因为@Query注释优先于方法名称的查询。

@Query docs

Upd:

from Spring docs:

来自Spring docs:

Although getting a query derived from the method name is quite convenient, one might face the situation in which ... the method name would get unnecessarily ugly. So you can either use JPA named queries through a naming convention ... or rather annotate your query method with @Query.

虽然获取从方法名称派生的查询非常方便,但是可能会面临这样的情况:方法名称会变得不必要地丑陋。因此,您可以通过命名约定使用JPA命名查询...或者使用@Query注释查询方法。

#1


Use default Java 8 feature for wrapping, just like so:

使用默认的Java 8功能进行换行,如下所示:

// use findOneByEmail instead
User findOneByDeletedIsFalseAndEmail(String email);

default User findOneByEmail(String email) {
    return findOneByDeletedIsFalseAndEmail(email);
}

See an example.

看一个例子。

#2


Now you can use Java 8 default interface methods as @Maksim Kostromin described. But there is no such a feature in Spring.

现在您可以使用Java 8默认接口方法,如@Maksim Kostromin所述。但是春天没有这样的功能。

-- Old answer

- 老答案

There is no such a way. You can specify any name for a method and add an annotation @Query with parameter value which holds desired query to database like this:

没有这样的方法。您可以为方法指定任何名称,并使用参数值添加注释@Query,该参数值将所需的查询保存到数据库,如下所示:

@Query(value="select u from User u where u.deleted=false and u.email=:email")
User findOneByEmail(@Param("email")String email);

or, with native sql query:

或者,使用本机SQL查询:

@Query(value="SELECT * FROM users WHERE deleted=false AND email=?1", nativeQuery=true)
User findOneByEmail(String email);

You can also use names that follow the naming convention for queries since @Query annotation will take precedence over query from method name.

您还可以使用遵循命名约定的名称进行查询,因为@Query注释优先于方法名称的查询。

@Query docs

Upd:

from Spring docs:

来自Spring docs:

Although getting a query derived from the method name is quite convenient, one might face the situation in which ... the method name would get unnecessarily ugly. So you can either use JPA named queries through a naming convention ... or rather annotate your query method with @Query.

虽然获取从方法名称派生的查询非常方便,但是可能会面临这样的情况:方法名称会变得不必要地丑陋。因此,您可以通过命名约定使用JPA命名查询...或者使用@Query注释查询方法。