Spring JPA data, multiple methods with same functionality

Viewed 596

I am using spring data jpa to interact with a database, however I've come across a problem: I can't define the same method multiple times with different named entities.

Consider:

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);
}

I'd like to have separate methods with different named graphs, but adding additional information to the name of the methods breaks spring. How can this be solved?

1 Answers

As comment suggests naming the methods correctly will not "break Spring". You can have:

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);
}

Or maybe you want to break stuff in two repositories, like:

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

and

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