gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:27:19 27 4
gpt4 key购买 nike

我正在使用 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);
}

我希望使用不同命名图的单独方法,但是向方法名称添加额外信息会破坏 spring.如何解决?

最佳答案

正如评论所建议的那样,正确命名方法不会“破坏 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);
}

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

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

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

关于java - Spring JPA数据,具有相同功能的多种方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64209863/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com