我正在使用 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/