-->
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.  [ 4 posts ] 
Author Message
 Post subject: possible bug: query specified join fetching, but the owner..
PostPosted: Tue Aug 11, 2009 11:24 am 
Newbie

Joined: Mon Jan 17, 2005 5:43 am
Posts: 13
Location: Macau
An Order contains some OrderItem's. Each OrderItem contains a Product and a quantity. To retrieve the Orders, The following HQL works:
Code:
from Order o left join fetch o.items i join fetch i.product


However, if I specify the select clause:
Code:
select o from Order o left join fetch o.items i join fetch i.product


Then Hibernate will return an error:
Code:
Exception in thread "main" org.hibernate.QueryException: query specified join fetching, but the owner of the fetched association was not present in the select list [FromElement{explicit,not a collection join,fetch join,fetch non-lazy properties,classAlias=null,role=null,tableName=Product,tableAlias=product2_,origin=items items1_,colums={items1_.p_id ,className=lab3.Product}}] [select o from lab3.Order o left join fetch o.items i join fetch i.product]
   at org.hibernate.hql.ast.tree.SelectClause.initializeExplicitSelectClause(SelectClause.java:217)
   at org.hibernate.hql.ast.HqlSqlWalker.useSelectClause(HqlSqlWalker.java:727)
   at org.hibernate.hql.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:551)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:645)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:251)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94)
   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135)
   at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342)
   at $Proxy0.createQuery(Unknown Source)
   at lab3.OnlineStoreApp.run(OnlineStoreApp.java:32)
   at lab3.OnlineStoreApp.main(OnlineStoreApp.java:14)


The test code is shown below:
Code:
package lab3;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class OnlineStoreApp {
   private SessionFactory factory;
   private Session session;

   public static void main(String[] args) {
      new OnlineStoreApp().run();
   }

   public OnlineStoreApp() {
      Configuration cfg = new Configuration();
      cfg.configure();
      factory = cfg.buildSessionFactory();
   }

   @SuppressWarnings("unchecked")
   private void run() {
      session = factory.getCurrentSession();
      session.beginTransaction();
      Order o = new Order();
      o.getItems().add(new OrderItem(new Product("p1"), 10));
      o.getItems().add(new OrderItem(new Product("p2"), 20));
      session.save(o);
      List<Order> orders = session
            .createQuery(
                  "select o from Order o left join fetch o.items i join fetch i.product")
            .list();
      System.out.println(orders.size());
      session.getTransaction().commit();
   }

}

package lab3;

import java.util.ArrayList;
import java.util.List;

public class Order {
   private Long internalId;
   private List<OrderItem> items;

   public Order() {
      items = new ArrayList<OrderItem>();
   }

   public Long getInternalId() {
      return internalId;
   }

   public void setInternalId(Long internalId) {
      this.internalId = internalId;
   }

   public List<OrderItem> getItems() {
      return items;
   }

   public void setItems(List<OrderItem> items) {
      this.items = items;
   }

}

package lab3;

public class OrderItem {
   private Product product;
   private int qty;

   public OrderItem() {
   }

   public OrderItem(Product product, int qty) {
      this();
      this.product = product;
      this.qty = qty;
   }

   public Product getProduct() {
      return product;
   }

   public void setProduct(Product product) {
      this.product = product;
   }

   public int getQty() {
      return qty;
   }

   public void setQty(int qty) {
      this.qty = qty;
   }


}

package lab3;


public class Product {
   private Long internalId;
   private String name;

   public Product() {
   }

   public Product(String name) {
      this();
      this.name = name;
   }

   public Long getInternalId() {
      return internalId;
   }

   public void setInternalId(Long internalId) {
      this.internalId = internalId;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }


}

<?xml version="1.0"?>
<!DOCTYPE hibernate-configuration PUBLIC
   "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <property name="hibernate.connection.url">jdbc:h2:tcp://localhost/~/test</property>
      <property name="hibernate.connection.driver_class">org.h2.Driver</property>
      <property name="hibernate.connection.username">sa</property>
      <property name="hibernate.connection.password"></property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
      <property name="hibernate.current_session_context_class">thread</property>
      <property name="hibernate.show_sql">false</property>
      <mapping resource="Product.hbm.xml"/>
      <mapping resource="Order.hbm.xml"/>
   </session-factory>
</hibernate-configuration>    

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="lab3.Order" table="orders">
      <id name="internalId">
         <generator class="sequence"></generator>
      </id>
      <list name="items" cascade="save-update">
         <key column="o_id"></key>
         <list-index column="idx"></list-index>
         <composite-element class="lab3.OrderItem">
            <many-to-one name="product" column="p_id" cascade="save-update"></many-to-one>
            <property name="qty"></property>
         </composite-element>
      </list>
   </class>
</hibernate-mapping>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="lab3.Product">
      <id name="internalId">
         <generator class="sequence"></generator>
      </id>
      <property name="name"></property>
   </class>
</hibernate-mapping>



Top
 Profile  
 
 Post subject: Re: possible bug: query specified join fetching, but the owner..
PostPosted: Wed Aug 12, 2009 9:17 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Quote:
query specified join fetching, but the owner of the fetched association was not present in the select list


No, that's expected behavior with this scenario if the owner of the fetched association was not present in the select list. You may have to take a different approach.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject: Re: possible bug: query specified join fetching, but the owner..
PostPosted: Wed Aug 12, 2009 9:05 pm 
Newbie

Joined: Mon Jan 17, 2005 5:43 am
Posts: 13
Location: Macau
Why having "select o" makes any difference? Both query will select the orders only (as using "foo join fetch ..." will only return foo's).

In addition, "o" is exactly the owner object.


Top
 Profile  
 
 Post subject: Re: possible bug: query specified join fetching, but the owner..
PostPosted: Wed Sep 16, 2009 11:05 am 
Newbie

Joined: Wed Sep 16, 2009 11:03 am
Posts: 1
I am also encountering this error. Does Hibernate/HQL not allow you to select only specific parts in the SELECT statement when you are using 'join fetch'?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.