Created
June 28, 2021 11:02
-
-
Save Danny-Hoang/65112f08dfd7ad6a05e662e1b74d1765 to your computer and use it in GitHub Desktop.
JPA query samples
This file contains hidden or 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 DataArticleRepository extends CrudRepository<Article, UUID> { | |
| Optional<Article> findBySlug(String slug); | |
| Optional<Article> findByTitle(String title); | |
| @Query("SELECT DISTINCT article_ FROM Article article_ " + | |
| "LEFT JOIN article_.tags t " + | |
| "LEFT JOIN article_.author p " + | |
| "LEFT JOIN article_.favoredUsers f " + | |
| "WHERE " + | |
| "(:tag IS NULL OR t.name = :tag) AND " + | |
| "(:author IS NULL OR p.username = :author) AND " + | |
| "(:favorited IS NULL OR f.username = :favorited)") | |
| List<Article> findByFilters(@Param("tag") String tag, | |
| @Param("author") String author, | |
| @Param("favorited") String favorited, | |
| Pageable pageable); | |
| @Query("SELECT article_ FROM Article article_ JOIN article_.author au WHERE au.id IN :followees") | |
| List<Article> findByFollowees(List<UUID> followees, Pageable pageable); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment