本文介绍了休眠联接错误:org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement处为NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目(快餐店)中创建此总部,以搜索以用户选择的商品为参数的所有订单:

I create this hql in my project (an snack bar), to search all orders that have the product selected by the user as parameter:

select order from Order order, OrderItem item 
inner join order.cod_order_item as item 
inner join item.cod_product as cod_product 
where cod_product = id

但是,当我运行createQuery()时,在org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement处提供了一个空指针.

However, when I run the createQuery(), gives a nullpointer at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement.

我在做什么错了?

下面是我的代码:

OrderDAO.java

OrderDAO.java

public class OrderDAO {

    private Session session;

    public PedidoDAO(Session session){
        this.session = session;
    }


    public List<Order> getAllOrderFromProduct(Product product{

        String hql = "select order from Order order, OrderItem item " +
                "inner join order.order_item_id as item " +
                "inner join item.product_id as product_id " +
                "where product_id = '"+ product.getId() + "'";

        Configuration cfg = new Configuration();

        SessionFactory factory = cfg.configure().buildSessionFactory();

        Session session = factory.openSession();

        Query query = session.createQuery(hql); 

        List result = query.list();

        return result;
    }

}

Order.java(实体)

Order.java (entity)

@Entity
public class Order{

    @Id
    @GeneratedValue
    private Long order_id;

    @Column(name="order_date", nullable=false, length=15)
    private Date data;

    @Column(name="order_total", nullable=false, length=8)
    private double total;

    /* Relacionamentos */

    @Column(name="employee_id", nullable=false, length=8)
    private Long employee_id;

    @Column(name="customer_id", nullable=false, length=8)
    private Long customer_id;

    @Column(name="order_item_id", nullable=false, length=8)
    private Long order_item_id;


    public Long getId() {
        return order_id;
    }

    public void setId(Long order_id) {
        this.order_id= order_id;
    }

    public Date getOrderDate() {
        return order_date;
    }

    public void setOrderDate(Date order_date) {
        this.order_date = order_date;
    }

    public double getTotal() {
        return total;
    }

    public void setTotal(double total) {
        this.total = total;
    }

    public Long getFuncionario() {
        return cod_funcionario;
    }

    public void setEmployee(Long employee_id) {
        this.employee_id= employee_id;
    }

    public Long getCustomer() {
        return customer_id;
    }

    public void setCustomer(Long customer_id) {
        this.customer_id= customer_id;
    }

    public Long getOrderItem() {
        return order_item_id;
    }

    public void setOrderItem(Long order_item_id) {
        this.order_item_id= order_item_id;
    }

}

我的hibernate.cfg.xml

My hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<hibernate-configuration>

<session-factory>

    <property name="connection.url">jdbc:mysql://localhost:3306/lanchonete_db</property>

    <property name="connection.username">root</property>
    <property name="connection.password"></property>

    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
    <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>


    <!-- Enable Hibernate's automatic session context management -->
    <property name="hibernate.current_session_context_class">thread</property>


    <!-- this will show us all sql statements -->
    <property name="hibernate.show_sql">true</property>


    <!-- mapping files -->

    <mapping class="gigabyte.bean.Customer" />
    <mapping class="gigabyte.bean.Address"/>
    <mapping class="gigabyte.bean.Employee" />
    <mapping class="gigabyte.bean.Order"/>
    <mapping class="gigabyte.bean.OrderItem" />
    <mapping class="gigabyte.bean.Product"/>
    <mapping class="gigabyte.bean.Phone" />

    </session-factory>

   </hibernate-configuration>

欢迎任何帮助.

推荐答案

我发现了我的错误!我忘了在Order.java的关系表中引用@ManyToMany注释,然后Hibernate尝试获取两个表之间的关系,但一无所获.现在,基于@axtavt答案,此查询可以正常使用:

I found my error! I forgot to reference the annotation @ManyToMany in relationship table on Order.java, then the Hibernate tried to get the relationship between the two tables and found nothing. Now, works fine with this query, based on @axtavt answer:

select order from Order order, OrderItem item
inner join order.order_item as item
where item.cod_product = id

我的Order.java已更正:

My Order.java corrected:

@Entity
   public class Order{

    @Id
    @GeneratedValue
    private Long order_id;

    @Column(name="order_date", nullable=false, length=15)
    private Date data;

    @Column(name="order_total", nullable=false, length=8)
    private double total;

    /* Relationships*/

    @Column(name="employee_id", nullable=false, length=8)
    private Long employee_id;

    @Column(name="customer_id", nullable=false, length=8)
    private Long customer_id;

    @ManyToMany(targetEntity=OrderItem.class, fetch=FetchType.LAZY)
    @Fetch(FetchMode.SUBSELECT)
    @JoinTable(name = "order_order_item", joinColumns = { @JoinColumn(name = "cod_order") }, 
    inverseJoinColumns = { @JoinColumn(name = "cod_item") })
        public Set<OrderItem> setOrderItem = new HashSet<OrderItem>();


    public Long getId() {
       return order_id;
    }

    public void setId(Long order_id) {
       this.order_id= order_id;
    }

    public Date getOrderDate() {
       return order_date;
    }

    public void setOrderDate(Date order_date) {
       this.order_date = order_date;
    }

    public double getTotal() {
       return total;
    }

    public void setTotal(double total) {
       this.total = total;
    }

    public Long getFuncionario() {
       return cod_funcionario;
    }

    public void setEmployee(Long employee_id) {
       this.employee_id= employee_id;
    }

    public Long getCustomer() {
       return customer_id;
    }

    public void setCustomer(Long customer_id) {
       this.customer_id= customer_id;
    }

    public Set<OrderItem> getOrderItem() {
       return orderItem;
    }

    public void setOrderItem(Set<OrderItem> orderItem) {
       this.orderItem= orderItem;
    }

 }

这篇关于休眠联接错误:org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement处为NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 17:15