1

I'm working with mySQL and I have the API using spring data. Entities and Repositories are configured and working. I have a football matches table called "Matches" and each match has a homeTeam and an awayTeam.

I need to retrieve a list of all matches where "Team 1" and "Team 2" have played so I can build their "match history"

Here is my repository:

public interface MatchRepository extends JpaRepository<Match,Long> {

    List<Match> findByHomeTeamOrAwayTeam(
            @Param("homeTeam") Team homeTeam,
            @Param("awayTeam") Team awayTeam
    );
}

This works fine, it returns every match where "Team 1" was the homeTeam and "Team 2" was the awayTeam. I can also do findByHomeTeamAndAwayTeam and it returns the same list but filtered by the matches they both played.

Now the problem I can't get around is how can I use the "IN" keyword to fetch all matches that had "Team 1" and "Team 2" despite home or away situation.

For now I'm doing 2 calls to the same endpoint inverting the parameters but is there a way to represent the following???:

Select * from matches where homeTeam IN (1, 2) AND awayTeam IN (1, 2);
0

1 Answer 1

2

Try to use In

findByHomeTeamIdInAndAwayTeamIdIn(List<Long> homeTeams, List<Long> awayTeams);

https://docs.spring.io/spring-data/jpa/docs/1.5.0.RELEASE/reference/html/jpa.repositories.html

Sign up to request clarification or add additional context in comments.

4 Comments

That seems like it will work if I'm able to pass a list as parameters. What is the syntax to pass a Collection or a List as a parameter in that JSON?
you mean that you want to pass a list of integers either? or how to oast the list itself ?
Yes so my problem is that team 1 and 2 are actually "localhost:4200/teams/1" and "localhost:4200/teams/31" In order to request it right now, I'm doing a GET to: (For example) localhost:8080/matches/search/…
Ahhh now it did work, Appearantly my (or yours) implementation was working, the problem was that it didn't get two urls as parameters. Now with the IDs, it is working great. Thanks a lot @Amr!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.