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

huangapple go评论83阅读模式
英文:

ClassCastException: org.hibernate.hql.internal.ast.tree.SqlNode cannot be cast to 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;
    }
}
英文:

I have service which connects to maria db in order to fetch data. I have the following method in my repository to fetch order data:

@Query(&quot;select new Order(o.orderNumber, coalesce(o.orderId, &#39;&#39;), coalesce(o.env, &#39;&#39;), coalesce(o.poNumber, &#39;&#39;), coalesce(o.qty, &#39;&#39;), coalesce(o.sku, &#39;&#39;), &quot;
            + &quot; coalesce(o.customerNumber, &#39;&#39;), coalesce(o.portfolio, &#39;&#39;), coalesce(o.custEmail, &#39;&#39;)) &quot;
            + &quot;from Order o where o.portfolio=?1 order by o.orderNumber desc&quot;)
    public List&lt;Order&gt; findAllOrder(String portfolio);

When I try to start the service, it throws me following error:

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

Any ideas on how to fix this? Appreciate the help.

Entity:

@Entity
@Table(name = &quot;orders&quot;)
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;
    }
}

答案1

得分: 2

@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);
英文:

Use full reference(with the package name) of your class in @Query.

Suppose your Order class is in your com.earth.project package then query will be

@Query(&quot;select new com.earth.project.Order(o.orderNumber, coalesce(o.orderId, &#39;&#39;), coalesce(o.env, &#39;&#39;), coalesce(o.poNumber, &#39;&#39;), coalesce(o.qty, &#39;&#39;), coalesce(o.sku, &#39;&#39;), &quot;
            + &quot; coalesce(o.customerNumber, &#39;&#39;), coalesce(o.portfolio, &#39;&#39;), coalesce(o.custEmail, &#39;&#39;)) &quot;
            + &quot;from Order o where o.portfolio=?1 order by o.orderNumber desc&quot;)
    public List&lt;Order&gt; findAllOrder(String portfolio);

huangapple
  • 本文由 发表于 2020年4月6日 16:09:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61055465.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定