简体   繁体   中英

ClassCastException: java.util.Date cannot be cast to

I am using service in CUBA-FRAMEWORK to do some data manipulation and getting the following error:

ClassCastException: java.util.Date cannot be cast to com.company.gms.entity.ProductionPlanResource

the error is originated in the following line of code:

matReqDate = DateUtils.addDays((Date)planQuery.getFirstResult().getStartDate(), daysOffset);
                                            }

additional codes around :

    Date reqShipDate = soline.getRequiredShipDate();
                                                Date matReqDate;
                                                TypedQuery<ProductionPlanResource> planQuery = persistence.getEntityManager()
                                                        .createQuery("select MIN(e.startDate) from mydb$ProductionPlanResource e " +
                                                                " where e.productionPlan.salesOrder.id = ?1 AND e.article.id = ?2", ProductionPlanResource.class);
                                                        planQuery.setParameter(1, soline.getSalesOrder().getId()).setParameter(2, article.getId());

                                                if (planQuery.getResultList().size() > 0) {

                                                    matReqDate = DateUtils.addDays((Date)planQuery.getFirstResult().getStartDate(), daysOffset);
                                                }

I tried this, but didn't help

java.sql.Date startDate = (java.sql.Date)planQuery.getFirstResult().getStartDate();

Thanks for helping.

您的TypedQuery预计将返回ProductionPlanResource但查看查询本身,它会返回MIN(e.startDate) ,它似乎是一个日期,而不是ProductionPlanResource

The exception tells You that there is a Date which is cast to ProductionPlanResource. That is the problem.

TypedQuery<ProductionPlanResource> planQuery =  ...
...select MIN(e.startDate) from ...

The result is a Date and the TypedQuery is for type ProductionPlanResource.

Try change to:

TypedQuery<Date> planQuery

您可以尝试.getTime()然后进行转换,因为java和sql date均读取了long值。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM