List is not being initialize when using NEW in JPQL

1.8k views Asked by At

I have a rather simple query that takes input for a few tables. When I try and run the query it says:

[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

JPQL query:

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

ShopOrder.java

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;

    }

I created a Juint test to see what was going on by doing the following:

@Test
@Transactional
public void shopOrderTest() throws Exception
{

        Po po = new Po();
        Long id = (long) 11;
        LocalDate date = new LocalDate(2015,6,10);
        po.setDue_date(date);
        po.setId(id);
        po.setPo_number("11254");
        po.setSales_order_number("34879");
        po.setStatus("ordered");
        BigDecimal total_sale = new BigDecimal("55");
        po.setTotal_sale(total_sale);

        Part part = new Part();
        BigDecimal brakepress_hrs_per_part = new BigDecimal("34");
        part.setBrakepress_hrs_per_part(brakepress_hrs_per_part);
        BigDecimal grind_hrs_per_part = new BigDecimal("354");
        part.setGrind_hrs_per_part(grind_hrs_per_part);
        part.setId(id);
        part.setInventory_count(55);
        BigDecimal laser_hrs_per_part = new BigDecimal("987");
        part.setLaser_hrs_per_part(laser_hrs_per_part);
        BigDecimal lb_per_part = new BigDecimal("58748");
        part.setLb_per_part(lb_per_part);

        poRes.save(po);
        partRes.save(part);

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

        int size = getOrders.size();

        System.out.println("The size is-----"+size);

        for(ShopOrder order: getOrders){
            System.out.println(order);
        }
    }

}

Now the test passes but the size of the List is 0 which explains why I am getting a null error. However I do not understand why.

The method that is giving me the null error is here:

/**
 * Generate Shop Orders.
 */
@SuppressWarnings("null")
@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);
    }

}

Po.java

@OneToMany(mappedBy="po",targetEntity=Po_part.class)
private List<Po_part> partList;


public List<Po_part> getPartList() {
    return partList;
}

Part.java

@OneToMany(mappedBy="part",targetEntity=Po_part.class)
private List<Po_part> partList;

Po_part.java

@ManyToOne
private Part part;

@ManyToOne
private Po po;

I hope this clears things up with my query. If you have any suggestions on how I should do a better query I am all ears.

Here is a picture of my database tables too:

Database tables

1

There are 1 answers

0
Satish Patro On
@Query("SELECT NEW com.htd.domain.ShopOrder(pp.po.id, pp.po.po_number, pp.po.due_date, pp.id, pp.part_quantity, "
        + "pp.part.id, pp.part.part_number, pp.part.part_description, pp.part.plasma_hrs_per_part, pp.part.grind_hrs_per_part, "
        + "pp.part.mill_hrs_per_part, pp/part.brakepress_hrs_per_part) "
        + "FROM Po_part pp Where pp.part.id = ?1")
List<ShopOrder> getShopOrder(Long id);

without join

@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_part po_part ON po_part.po.id = po.id"
        + "LEFT JOIN Part part ON po_part.part.id = part.id AND po.part.id = ?1")
List<ShopOrder> getShopOrder(Long id);

with Left join