有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Hibernate连接错误:组织上的NullPointerException。冬眠hql。阿斯特。HqlSqlWalker。createFromJoinElement

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

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上给出一个null指针。冬眠hql。阿斯特。HqlSqlWalker。createFromJoinElement

我做错了什么

下面是我的代码:

命令道。爪哇

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;
    }

}

秩序。java(实体)

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

}

我的冬眠。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>

欢迎任何帮助


共 (2) 个答案

  1. # 1 楼答案

    您当然不需要显式地将OrderItem添加到from,因为它已经被join添加了:

    select order from Order order 
    inner join order.cod_order_item as item 
    inner join item.cod_product as cod_product 
    where cod_product = id
    
  2. # 2 楼答案

    我发现了我的错误!我忘了在Order的关系表中引用注释@ManyToMany。java,然后Hibernate尝试获取两个表之间的关系,但什么也没有找到。现在,基于@axtavt answer,可以很好地处理此查询:

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

    我点的菜。java更正:

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