-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Query related entities with EJB QL (SQL performance)
PostPosted: Tue Jan 13, 2009 8:18 am 
Newbie

Joined: Tue May 06, 2008 4:19 am
Posts: 6
I am using JPA with Hibernate3.3.x, I have a simple OneToMany mapping, eg,

Code:
 
  Order uses OneToMany annotation to define relationship with its OrderLineItems


Surely I can do a query to get an Order along with all its OrderLineItems shi way
Code:
    List<Order> orderList = em.createQuery(
            "select o from Order o where o.id=:id").
            setParameter("id", orderId).getResultList();


But how can I only want the OrderLineItem list? Can I use the following code?

Code:
    List<OrderLineItem> itemList = em.createQuery(
            "select i from OrderLineItem i inner join Order o where o.id=:id").
            setParameter("id", orderId).getResultList();


Last edited by DanielYWoo on Tue Jan 13, 2009 9:17 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 13, 2009 8:30 am 
Newbie

Joined: Tue May 06, 2008 4:19 am
Posts: 6
I think I can make it by

Code:
    return em.createQuery(
   "select i from OrderLineItem i where i.order.id = :orderId").
   setParameter("orderId", orderId).getResultList();


But that causes Hibernate execute an unnecessary SQL as below (the first one is not necessary)

Code:
select *
    from orderlineitem where orderlineitem.orderid=?
       
select *
    from orderlineitem
    where orderlineitem.order_id=? order by ...


I can also make it by another query
Code:
    return em.createQuery(
   "select o.orderLineItemList from Order o where o.id = :orderId").
   setParameter("orderId", orderId).getResultList();

But this also gives an unnecessary SQL like this:
Code:
    select *
    from oder inner join orderlineitem on order.id=orderlineitem.orderid
    where order.id=?

    select *
    from orderlineitem
    where orderlineitem.orderid=? order by ...




How can I get only one query like
Code:
select * from orderlineitem where orderlineitem.orderid=?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.