Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate join error: NullPointerException at org.hibernate.hql.ast.HqlSqlWalker.createFromJoinElement

Tags:

java

hibernate

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

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

What am i doing wrong?

Below, here's my codes:

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)

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

}

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>

Any help is welcome.

like image 405
mari Avatar asked Jan 21 '13 15:01

mari


1 Answers

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

 }
like image 97
mari Avatar answered Sep 22 '22 01:09

mari