Spring Data Jpa QueryException:序数参数不是以基数1开始的:2

s4chpxco  于 2023-08-05  发布在  Spring
关注(0)|答案(1)|浏览(129)

你好,我试图通过枚举过滤,但我一直有不同类型的错误,所以我切换到本地查询,但仍然遇到了错误。
以下是错误:org.hibernate.QueryException:序数参数不是以基数1开始的:2它指向了我的服务方法...我在google上搜索过,但没有类似的问题。
资料档案库:

@Query(value=
"""
    SELECT mi.* 
    FROM meal_item AS mi 
    WHERE 
        (mi.meal LIKE '%?1%' OR 
        mi.note LIKE '%?1%') AND 
        (?2 IS NULL OR
        ?2 = mi.meal_size)             
""", nativeQuery=true)
List<MealItem> getAllMealItems(String search, MealItem.MealSize mealSize, Pageable pageable);

字符串
服务内容:

@Override
public List<MealItem> getAllMealItems(String search, Pageable pageable) {
    return mealItemRepository.getAllMealItems(search, MealItem.MealSize.light, pageable);
}


MealItem枚举:

public enum MealSize{
    light, medium, heavy
}

@Enumerated(EnumType.STRING)
@JsonProperty("size")
@Column(name="meal_size", columnDefinition="ENUM('light','medium','heavy')", nullable=false)
private MealSize mealSize;


这是切换到本机查询后的新错误...

6yt4nkrj

6yt4nkrj1#

我已经通过切换到命名参数并修复查询中的引号解决了这个问题...

@Query(value=
    """
        SELECT mi.* 
        FROM meal_item AS mi 
        WHERE 
            (mi.meal LIKE CONCAT('%', :s, '%') OR 
            mi.note LIKE CONCAT('%', :s, '%')) AND 
            (:#{#ms?.name()} IS NULL OR
            :#{#ms?.name()} = mi.meal_size)             
    """, nativeQuery=true)
    List<MealItem> getAllMealItems(@Param("s") String search, @Param("ms") MealItem.MealSize mealSize, Pageable pageable);

字符串

相关问题