如何使用HQL在WHERE子句中使用可选参数?

时间:2018-06-15 09:42:24

标签: java spring hibernate spring-data-jpa spring-data

我希望能够执行过滤可用行的查询,如果不可用,则返回所有结果。如何在纯HQL中完成此操作,而不使用Criteria

下面的代码段会编译,但查询不会返回任何结果。

@Repository
public interface ArticleRepository extends CrudRepository<Article, Long> {

    @Query("FROM Article a " +
            "JOIN a.manufacturer m " +
            "WHERE " +
            "(:search_term IS NULL OR :search_term = '' OR a.description LIKE CONCAT(:search_term, '%')) AND " +
            "(:manufacturer_id IS NULL OR m.id = :manufacturer_id) " +
            "ORDER BY f.priority DESC")
    Page<Firmware> search(@Nullable @Param("search_term") String searchTerm,
                          @Nullable @Param("manufacturer_id") Long manufacturerId,
                          Pageable pageable);

}

0 个答案:

没有答案