【问题标题】:How to search if bytearray contains element from other array如何搜索字节数组是否包含来自另一个数组的元素
【发布时间】:2020-03-24 11:39:20
【问题描述】:

我有一个名为 Recipe 的实体,我创建了一个扩展 JpaRepository 的存储库,我想搜索包含 DietLabelList 搜索数组的每个元素的食谱。

@Getter
@Setter
@Entity
@NoArgsConstructor
@RequiredArgsConstructor
@ToString
@Table(name = "recipe")
public class Recipe {

    @Column(name = "id", nullable = false)
    @Id
    @GeneratedValue
    private UUID rid;

    @Column(name = "title", nullable = false)
    private String title;

    @Column(name = "dietLabelList", nullable = false)
    private UUID[] dietLabelList;

}
@Repository
public interface RecipeRepository extends JpaRepository<Recipe, UUID> {

    List<Recipe> findByTitleContaining(String title);
    List<Recipe> findByDietLabelList(UUID[] dietLabels);

}

例如我有一个食谱,它有一个像这样的 DietLabelList ["Balanced", "High-Fiber", "High-Protein"] 和 findByDietLabelList(["Balanced", "High-Fiber"]) 应该能够找到它. JpaRepository 可以实现类似的功能吗?

【问题讨论】:

    标签: java spring postgresql hibernate jpa


    【解决方案1】:

    您可以使用QueryParam 并指定您的自定义查询

    import org.springframework.data.jpa.repository.Query;
    import org.springframework.data.repository.query.Param;
    
    @Repository
    public interface RecipeRepository extends JpaRepository<Recipe, UUID> {
    
        List<Recipe> findByTitleContaining(String title);
    
        @Query(value = "SELECT r FROM Recipe r WHERE r.dietLabels in :dietLabels")
        List<Recipe> findByDietLabelList(@Param("dietLabels") UUID[] dietLabels);
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 2016-05-02
      • 1970-01-01
      • 2011-02-02
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 2021-06-04
      • 2021-01-30
      相关资源
      最近更新 更多