当前位置: 首页 > 面试题库 >

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

鄂育
2023-03-14
问题内容

这已经发布过,但是我的问题有点不同。这是有问题的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);
        }

    }

忠告?

------------ UPDATE ----------------

这似乎解决了这个问题:

@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

ShopOrder:

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;

    }

问题答案:

而不是使用: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);


 类似资料:
  • 问题内容: 最近从这个问题中我了解到以下内容似乎是合法的java: 现在,我试图找到在Java标准中允许您执行此操作的位置,并查看了此处,但找不到该部分。 有人可以引用允许这种形式的方法声明的地方,以及声明命名实参的限制是什么? 问题答案: 它在1.8或更高版本的JDK中有效。 这里是JLS说 接收器参数是实例方法或内部类的构造函数的可选语法设备。对于实例方法,接收器参数代表为其调用该方法的对象。

  • 错误:运算符不存在:uuid=bytea 有什么建议吗?

  • 问题内容: 我正在尝试进行设置,以便将表名作为参数传递给命令文本,但是我无法使其正常工作。我四处张望,发现了这样的问题:使用C#进行MySQL的参数化查询,但是我没有任何运气。 这是相关的代码(connection == 包含连接字符串): 如果我更改: 至: 作为一种测试方法,它是可行的(我正在将XML从数据集中写入控制台以进行检查)。因此,我很确定问题只是以错误的方式使用了参数功能。有指针吗?

  • 问题内容: 我试图在我的appsettings.json文件中写入连接字符串,并将其带入我的启动文件中,但我不断得到一个Value不能为null。参数名称:connectionString。我一直在使用各种示例,但似乎看不到带有ASP.NET 1.0 Core启动类的新设置。 Appsetting.json文件: 尝试Startup.cs的方法 问题答案: 首先, 与在Visual Studio中

  • 问题内容: 嗨,我有一个命名查询 我想这样设置限制: 但这在服务器启动时显示错误。我正在使用以下代码在DAO类中调用查询: 需要设置开始和结束参数。请帮忙。 问题答案: 正如@DataNucleus所说,LIMIT在JPQL中不是有效的关键字。这不是指定要返回多少行的方法。这是您的操作方式: 这将是调用命名查询的代码: