Spring JPA数据,具有相同功能的多种方法

Rob*_*bus 4 java spring-data-jpa

我正在使用 spring data jpa 与数据库交互,但是我遇到了一个问题:我无法使用不同的命名实体多次定义相同的方法。

考虑:

class Repository {
    @EntityGraph(value = UserEo.FULL, type = EntityGraph.EntityGraphType.LOAD)
    public Optional<UserEo> findUserEoByEmail/*Full*/(String email);

    @EntityGraph(value = UserEo.BRIEF, type = EntityGraph.EntityGraphType.LOAD)
    public Optional<UserEo> findUserEoByEmail/*Brief*/(String email);
}

Run Code Online (Sandbox Code Playgroud)

我想要具有不同命名图的单独方法,但是向方法名称添加附加信息会破坏 spring. 如何解决这个问题?

pir*_*rho 7

正如评论所暗示的那样,正确命名方法不会“破坏 Spring”。你可以有:

public interface Repository extends JpaRepository<UserEo, Long> {
    @EntityGraph(value = UserEo.FULL, type = EntityGraph.EntityGraphType.LOAD)
    public Optional<UserEo> findFullByEmail(String email);
    @EntityGraph(value = UserEo.BRIEF, type = EntityGraph.EntityGraphType.LOAD)
    public Optional<UserEo> findBriefByEmail(String email);
}
Run Code Online (Sandbox Code Playgroud)

或者也许您想破坏两个存储库中的内容,例如:

public interface RepositoryFull extends JpaRepository<UserEo, Long> {
    @EntityGraph(value = UserEo.FULL, type = EntityGraph.EntityGraphType.LOAD)
    public Optional<UserEo> findByEmail(String email);
}
Run Code Online (Sandbox Code Playgroud)

public interface RepositoryBrief extends JpaRepository<UserEo, Long> {
    @EntityGraph(value = UserEo.BRIEF, type = EntityGraph.EntityGraphType.LOAD)
    public Optional<UserEo> findByEmail(String email);
}
Run Code Online (Sandbox Code Playgroud)