How to search if bytearray contains element from other array

regis_studios :

I have an entity called Recipe and I created a Repository which extends JpaRepository and I would like to search up the recipes which contains every element of the search array for 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);

}

e.g. I have a recipe that has as a dietLabelList like this one ["Balanced", "High-Fiber", "High-Protein"] and findByDietLabelList(["Balanced", "High-Fiber"]) should be able to find it. Is something like possible with JpaRepository?

am0awad :

You can use QueryParam and specify your custom Query

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);
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=352861&siteId=1