簡體   English   中英

請建議自定義 findBy 或查詢

[英]Please, suggest custom findBy or query

我需要通過幾個參數進行搜索,但如果其中一些參數為null則在搜索時不要考慮它們。 例如,我想找到一個在美國並在 Google 工作的人,但現在我的搜索已實現,以便所有現在在美國或目前在 Google 工作的人。 如果我使用 and 而不是 or,將使用null搜索字段。

現在我的搜索看起來像這樣,並在輸入上得到一個集合——第二個問題由此產生。 如何將此集合與該實體的 OneToMany 表進行比較?

Long<Game> findByGameTitleOrGameTypeOrGameLocations(String title, String type, Set<String> locations);

這是來自實體的一組位置:

@ElementCollection
private Set<String> gameLocations;

這些問題對某些人來說可能很愚蠢 xD 但我是新手。

我可以通過 @Query 注釋建議自定義查詢請求。 我不確定這是最佳方式,但它有效。

@Query("SELECT g FROM Game g" +
    " WHERE (:title is null or g.title = :title)" +
    " AND (:type is null or g.type = :type)" +
    " AND (:locations is null or g.locations in :locations)")
Long<Game> findByGameTitleOrGameTypeOrGameLocations(
    @Param("title") String title,
    @Param("type") String type,
    @Param("locations") Set<String> locations);

這是一個請求示例,因為我不完全了解您的數據庫模型。

why cant you go with preparing the query as below.
@Query(value = "select * from game ga where ga.tile=:#{#title}  and ga.type=:#{#type} and ga.location in (:#{#locationList})", nativeQuery = true)
    Object[] findByGameTitleOrGameTypeOrGameLocations(@Param("title") String title,@Param("type") String type,@Param("location") List<String> locationList);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM