5

我在我的项目(小吃店)中创建了这个 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.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement 处给出一个空指针。

我究竟做错了什么?

下面,这是我的代码:

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(实体)

@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

<?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>

欢迎任何帮助。

4

2 回答 2

5

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

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

 }
于 2013-01-23T12:51:34.423 回答
0

您当然不需要显式添加OrderItemfrom因为它已经被添加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
于 2013-01-21T15:32:48.900 回答