首页 > 解决方案 > ClassCastException: org.hibernate.hql.internal.ast.tree.SqlNode 不能转换为 org.hibernate.hql.internal.ast.tree.PathNode

问题描述

我有连接到 maria db 以获取数据的服务。我的存储库中有以下方法来获取订单数据:

@Query("select new Order(o.orderNumber, coalesce(o.orderId, ''), coalesce(o.env, ''), coalesce(o.poNumber, ''), coalesce(o.qty, ''), coalesce(o.sku, ''), "
            + " coalesce(o.customerNumber, ''), coalesce(o.portfolio, ''), coalesce(o.custEmail, '')) "
            + "from Order o where o.portfolio=?1 order by o.orderNumber desc")
    public List<Order> findAllOrder(String portfolio);

当我尝试启动服务时,它会抛出以下错误:

ClassCastException: org.hibernate.hql.internal.ast.tree.SqlNode cannot be cast to org.hibernate.hql.internal.ast.tree.PathNode

有想法该怎么解决这个吗?感谢帮助。

实体:

@Entity
@Table(name = "orders")
public class Order {

    private Long orderNumber;
private String orderId;
private String env;
private String poNumber;
private String qty;
private String orderItem;
private String sku;
private String status;
private String portfolio;
private Long customerNumber;
private int requestId;
private int recordId;
private String custEmail;
 public Order(Long orderNumber, String orderId, String env, String poNumber, String qty, String sku,
            Long customerNumber, String portfolio, String custEmail) {
        this.orderNumber = orderNumber;
        this.orderId = orderId;
        this.env = env;
        this.poNumber = poNumber;
        this.qty = qty;
        this.sku = sku;
        this.customerNumber = customerNumber;
        this.portfolio = portfolio;
        this.custEmail = custEmail;
    }
}

标签: javahibernatespring-bootspring-data-jpa

解决方案


@Query.

假设您的Order课程在您的com.earth.project包中,那么查询将是

@Query("select new com.earth.project.Order(o.orderNumber, coalesce(o.orderId, ''), coalesce(o.env, ''), coalesce(o.poNumber, ''), coalesce(o.qty, ''), coalesce(o.sku, ''), "
            + " coalesce(o.customerNumber, ''), coalesce(o.portfolio, ''), coalesce(o.custEmail, '')) "
            + "from Order o where o.portfolio=?1 order by o.orderNumber desc")
    public List<Order> findAllOrder(String portfolio);

推荐阅读