我在我的项目(小吃店)中创建了这个 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>
欢迎任何帮助。