参数绑定的名称不能为空或空!对于命名参数,您需要在 Java 版本上使用 @Param 查询方法参数

Posted

技术标签:

【中文标题】参数绑定的名称不能为空或空!对于命名参数,您需要在 Java 版本上使用 @Param 查询方法参数【英文标题】:Name for parameter binding must not be null or empty! For named parameters you need to use @Param for query method parameters on Java versions 【发布时间】:2015-08-27 14:01:30 【问题描述】:

这已经发布过,但我的问题有点不同。这是有问题的 JPQL 查询:

@Query("SELECT NEW com.htd.domain.ShopOrder(po.id, po.po_number, "
            + "po.due_date, po_part.id, po_part.part_quantity, "
            + "part.id, part.part_number, part.part_description, "
            + "part.plasma_hrs_per_part, part.grind_hrs_per_part, "
            + "part.mill_hrs_per_part, part.brakepress_hrs_per_part) "
            + "FROM Po po "
            + "LEFT JOIN po.partList po_part "
            + "LEFT JOIN po_part.part part "
            + "LEFT JOIN po_part.part where po.id = :id")
    List<ShopOrder> getShopOrder(long id);

现在我确实尝试过:

@Query("SELECT NEW com.htd.domain.ShopOrder(po.id, po.po_number, "
            + "po.due_date, po_part.id, po_part.part_quantity, "
            + "part.id, part.part_number, part.part_description, "
            + "part.plasma_hrs_per_part, part.grind_hrs_per_part, "
            + "part.mill_hrs_per_part, part.brakepress_hrs_per_part) "
            + "FROM Po po "
            + "LEFT JOIN po.partList po_part "
            + "LEFT JOIN po_part.part part "
            + "LEFT JOIN po_part.part where po.id = :id")
    List<ShopOrder> getShopOrder(@Param(value="id"));

但这给了我一个警告:

    [ERROR] org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].  
 [dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in 
 context with path [] threw exception [Request processing failed; nested   
 exception is org.springframework.dao.InvalidDataAccessApiUsageException: 
 Name for parameter binding must not be null or empty! For named parameters 
 you need to use @Param for query method parameters on Java versions < 8.; 
 nested exception is java.lang.IllegalArgumentException: Name for parameter 
 binding must not be null or empty! For named parameters you need to use 
 @Param for query method parameters on Java versions < 8.] with root cause
java.lang.IllegalArgumentException: Name for parameter binding must not be 
null or empty! For named parameters you need to use @Param for query method   
parameters on Java versions < 8.

执行查询的方法是:

 /**
     * Generate Shop Orders.
     */
    @RequestMapping(value = "/generateShopOrder/id", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    @Timed
    public void generate(@PathVariable Long id) throws URISyntaxException 
        System.out.println("po id to generate = " + id);

        List<ShopOrder> shopOrders = po_partRepository.getShopOrder(id);

        for(ShopOrder order: shopOrders) 
            System.out.println("-------Printing Shop Orders" + order);
        

    

建议?

------------更新----

这似乎解决了问题:

@Query("SELECT NEW com.htd.domain.ShopOrder(po.id, po.po_number, "
            + "po.due_date, po_part.id, po_part.part_quantity, "
            + "part.id, part.part_number, part.part_description, "
            + "part.plasma_hrs_per_part, part.grind_hrs_per_part, "
            + "part.mill_hrs_per_part, part.brakepress_hrs_per_part) "
            + "FROM Po po "
            + "LEFT JOIN po.partList po_part "
            + "LEFT JOIN po_part.part part "
            + "LEFT JOIN po_part.part where po.id = ?1")
    List<ShopOrder> getShopOrder(Long id);

但是现在我收到一条错误消息:

[ERROR] org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/].
[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in 
context with path [] threw exception [Request processing failed; nested 
exception is org.springframework.dao.InvalidDataAccessApiUsageException: 
org.hibernate.QueryException: could not instantiate class 
[com.htd.domain.ShopOrder] from tuple; nested exception is 
java.lang.IllegalArgumentException: org.hibernate.QueryException: could not 
instantiate class [com.htd.domain.ShopOrder] from tuple] with root cause
java.lang.IllegalArgumentException: null

商店订单:

public ShopOrder(long po_id, String po_number, LocalDate po_due_date,
    long po_part_id, int part_quantity, long part_id,
    String part_number, String part_decription, BigDecimal plasma_hrs,
    BigDecimal grind_hours, BigDecimal mill_hrs,
    BigDecimal breakpress_hrs) 

        this.po_id = po_id;
        this.po_number = po_number;
        this.po_due_date = po_due_date;
        this.po_part_id = po_part_id;
        this.part_quantity = part_quantity;
        this.part_id = part_id;
        this.part_number = part_number;
        this.part_decription = part_decription;
        this.plasma_hrs = plasma_hrs;
        this.grind_hours = grind_hours;
        this.mill_hrs = mill_hrs;
        this.breakpress_hrs = breakpress_hrs;

    

【问题讨论】:

什么给了你错误?什么引发异常?堆栈跟踪? @NeilStockton 答案更新为完整堆栈跟踪 @NeilStockton 查看更新...... 所以也许 ShopOrder 没有这样的构造函数...检查类型 @NeilStockton 不存在(只是添加了它),但我开始认为我的查询中的“新”没有按照我的想法做。我认为它会创建一个新的 ShopOrder 实例。我说的对吗? 【参考方案1】:

不要使用 : po.id = :id 而是使用 ?1....

@Query("SELECT NEW com.htd.domain.ShopOrder(po.id, po.po_number, "
             + "po.due_date, po_part.id, po_part.part_quantity, "
            + "part.id, part.part_number, part.part_description, "
            + "part.plasma_hrs_per_part, part.grind_hrs_per_part, "
            + "part.mill_hrs_per_part, part.brakepress_hrs_per_part) "
            + "FROM Po po "
            + "LEFT JOIN po.partList po_part "
            + "LEFT JOIN po_part.part part "
            + "LEFT JOIN po_part.part where po.id = ?1")
    List<ShopOrder> getShopOrder(Long id);

【讨论】:

没错,他们官网上也提到过:链接-docs.spring.io/spring-data/jpa/docs/current/reference/html/… 否则 po.id = :id【参考方案2】:

试试这个参数说明:

List<ShopOrder> getShopOrder(@Param("id") long id);

【讨论】:

我觉得奇怪的是 JPA 不允许没有值的 @Param 标记使用与变量名称相同的默认参数名称。 @AmrinderArora 因为变量名不可访问 @er-han 是的,但肯定可以。例如:***.com/questions/40526925/… @AmrinderArora 是的,确实如此。谢谢你的例子。你是对的,它可以用'-parameters'编译器标志来完成。我发现了这个:docs.spring.io/spring-data/jpa/docs/current/reference/html/… @AmrinderArora 你说得对,-parameters 也可以解决问题。

以上是关于参数绑定的名称不能为空或空!对于命名参数,您需要在 Java 版本上使用 @Param 查询方法参数的主要内容,如果未能解决你的问题,请参考以下文章

java.lang.IllegalArgumentException:用户名不能为空或空,AbstractXMPPConnection.java:484

在 XSLT 中检查字符串是不是为空或空

在 Kotlin 中处理可为空或空列表的惯用方式

在 joi 和 express-validation 中允许字符串为空或空

检查对象中的值是不是为空或空javascript

评估空或空 JSTL c 标签