本文介绍了参数绑定的名称不能为null或为空!对于命名参数,您需要在Java版本上使用@Param作为查询方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This has been posted before but my issue is a little different. This is the JPQL query in question:

@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"));

但这给了我一个警告说:

But this giving me a warning saying:

    [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----------------

这似乎解决了这个问题:

This seemed to fix the issue:

@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);

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

However now I am getting an error saying:

[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:

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 ....

Instead of using : po.id = :id just use ?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);

这篇关于参数绑定的名称不能为null或为空!对于命名参数,您需要在Java版本上使用@Param作为查询方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 18:14