QueryException: 序数参数未从基础值1开始:2

huangapple go评论57阅读模式
英文:

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=
&quot;&quot;&quot;
    SELECT mi.* 
    FROM meal_item AS mi 
    WHERE 
    	(mi.meal LIKE &#39;%?1%&#39; OR 
        mi.note LIKE &#39;%?1%&#39;) AND 
        (?2 IS NULL OR
    	?2 = mi.meal_size)             
&quot;&quot;&quot;, nativeQuery=true)
List&lt;MealItem&gt; getAllMealItems(String search, MealItem.MealSize mealSize, Pageable pageable);

Service:

@Override
public List&lt;MealItem&gt; 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(&quot;size&quot;)
@Column(name=&quot;meal_size&quot;, columnDefinition=&quot;ENUM(&#39;light&#39;,&#39;medium&#39;,&#39;heavy&#39;)&quot;, 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=
    &quot;&quot;&quot;
        SELECT mi.* 
        FROM meal_item AS mi 
        WHERE 
        	(mi.meal LIKE CONCAT(&#39;%&#39;, :s, &#39;%&#39;) OR 
            mi.note LIKE CONCAT(&#39;%&#39;, :s, &#39;%&#39;)) AND 
            (:#{#ms?.name()} IS NULL OR
        	:#{#ms?.name()} = mi.meal_size)             

    &quot;&quot;&quot;, nativeQuery=true)
    List&lt;MealItem&gt; getAllMealItems(@Param(&quot;s&quot;) String search, @Param(&quot;ms&quot;) 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=
    &quot;&quot;&quot;
        SELECT mi.* 
        FROM meal_item AS mi 
        WHERE 
        	(mi.meal LIKE CONCAT(&#39;%&#39;, :s, &#39;%&#39;) OR 
            mi.note LIKE CONCAT(&#39;%&#39;, :s, &#39;%&#39;)) AND 
            (:#{#ms?.name()} IS NULL OR
        	:#{#ms?.name()} = mi.meal_size)             
    &quot;&quot;&quot;, nativeQuery=true)
    List&lt;MealItem&gt; getAllMealItems(@Param(&quot;s&quot;) String search, @Param(&quot;ms&quot;) MealItem.MealSize mealSize, Pageable pageable);

huangapple
  • 本文由 发表于 2023年5月22日 19:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76305570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定