英文:
QueryException: Ordinal parameters did not start with base 1 : 2
问题
Here is the translated code section:
Repository:
@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);
Service:
@Override
public List<MealItem> getAllMealItems(String search, Pageable pageable) {
return mealItemRepository.getAllMealItems(search, MealItem.MealSize.light, pageable);
}
MealItem enum:
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;
Please note that the error message you provided indicates an issue with ordinal parameters not starting with base 1, but the code you've shown uses parameter placeholders like ?1
and ?2
, which should work correctly in most cases. If you continue to face issues, you may need to debug further.
英文:
Hello I am trying to filter by enum but I keep having different kinds of errors so I have switched to native query, but still encountered an error.
Here is the error: org.hibernate.QueryException: Ordinal parameters did not start with base 1 : 2
It pointed to my service method... I have googled it but there are no similar questions...
Repository:
@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);
Service:
@Override
public List<MealItem> getAllMealItems(String search, Pageable pageable) {
return mealItemRepository.getAllMealItems(search, MealItem.MealSize.light, pageable);
}
MealItem enum:
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;
This is a new error after switching to native query...
答案1
得分: 0
I have solved this by switching to named parameters and fixing the quotes in the query...
@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);
Please note that the code is not translated as per your request.
英文:
I have solved this by switching to named parameters and fixing the quotes in the query...
@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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论