Last active
December 12, 2020 18:50
-
-
Save susimsek/c3cd1f1544fb24cdfb1e07212d326bee to your computer and use it in GitHub Desktop.
Spring Data Jpa Query Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface VoteRepository extends JpaRepository<Vote, Long> { | |
@Query("SELECT NEW com.example.polls.model.ChoiceVoteCount(v.choice.id, count(v.id)) FROM Vote v WHERE v.poll.id in :pollIds GROUP BY v.choice.id") | |
List<ChoiceVoteCount> countByPollIdInGroupByChoiceId(@Param("pollIds") List<Long> pollIds); | |
@Query("SELECT NEW com.example.polls.model.ChoiceVoteCount(v.choice.id, count(v.id)) FROM Vote v WHERE v.poll.id = :pollId GROUP BY v.choice.id") | |
List<ChoiceVoteCount> countByPollIdGroupByChoiceId(@Param("pollId") Long pollId); | |
@Query("SELECT v FROM Vote v where v.user.id = :userId and v.poll.id in :pollIds") | |
List<Vote> findByUserIdAndPollIdIn(@Param("userId") Long userId, @Param("pollIds") List<Long> pollIds); | |
@Query("SELECT v FROM Vote v where v.user.id = :userId and v.poll.id = :pollId") | |
Vote findByUserIdAndPollId(@Param("userId") Long userId, @Param("pollId") Long pollId); | |
@Query("SELECT COUNT(v.id) from Vote v where v.user.id = :userId") | |
long countByUserId(@Param("userId") Long userId); | |
@Query("SELECT v.poll.id FROM Vote v WHERE v.user.id = :userId") | |
Page<Long> findVotedPollIdsByUserId(@Param("userId") Long userId, Pageable pageable); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment