Skip to content

Instantly share code, notes, and snippets.

@Danny-Hoang
Created June 28, 2021 11:02
Show Gist options
  • Save Danny-Hoang/65112f08dfd7ad6a05e662e1b74d1765 to your computer and use it in GitHub Desktop.
Save Danny-Hoang/65112f08dfd7ad6a05e662e1b74d1765 to your computer and use it in GitHub Desktop.
JPA query samples
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