简体   繁体   English

如何将自定义部分添加到 spring-data 存储库方法名称?

[英]How to add a custom part to a spring-data Repository method name?

I have a repository and want to add two methods that perform "the same" operation (eg find by id), but one method eagerly fetches some data.我有一个存储库,想添加两种执行“相同”操作的方法(例如按 id 查找),但一种方法急切地获取一些数据。 Is it possible to add a custom part to the name to allow for both methods to exist and to inform the caller about what the methods do?是否可以在名称中添加自定义部分以允许两种方法同时存在并告知调用者这些方法的作用?

For example:例如:

@Repository
public interface UserRepository {

    Optional<User> findById(UUID id);
    
    @EntityGraph(attributePaths = {"posts", "comments"})
    Optional<User> findByIdEagerlyFetchingPostsAndComments(UUID id);
}

You need to place method marker ( EagerlyFetchingPostsAndComments in your case) between (find|read|get|query|search|stream) and By , for example:您需要在(find|read|get|query|search|stream)By之间放置方法标记(在您的情况下为EagerlyFetchingPostsAndComments ),例如:

@EntityGraph(attributePaths = {"posts", "comments"})
Optional<User> findEagerlyFetchingPostsAndCommentsById(UUID id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM