1 Reply Latest reply on Jul 19, 2006 8:23 AM by Stefan Craatz

    Can't fetch two lazy collections!?

    tsar_bomba Newbie

      I was rather surprised to see this exception:

      Caused by: org.hibernate.HibernateException: cannot simultaneously fetch multiple bags
       at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:66)
       at org.hibernate.loader.hql.QueryLoader.<init>(QueryLoader.java:100)
       at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)
       at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:110)
       at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
       at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
       at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
       at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
       at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
       at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1612)
       at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:76)
       ... 70 more
      


      I have an entity called Order which has two lazy collections:

      @Entity
      @Table(name="tbl_order")
      public class Order implements Serializable
      {
       private List<Address> addresses;
       private List<OrderLine> orderItems;
       private List<TrackingNumber> trackingNumbers;
      
       public Order()
       {
       }
      
       @OneToMany(mappedBy="order", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
       public List<Address> getAddresses()
       {
       return this.addresses;
       }
      
       public void setAddresses(List<Address> addresses)
       {
       this.addresses = addresses;
       }
      
       @OneToMany(mappedBy="order", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
       public List<OrderLine> getOrderItems()
       {
       return this.orderItems;
       }
      
       public void setOrderItems(List<OrderLine> orderItems)
       {
       this.orderItems = orderItems;
       }
      
       @OneToMany(mappedBy="order", fetch=FetchType.LAZY, cascade=CascadeType.ALL)
       public List<TrackingNumber> getTrackingNumbers()
       {
       return this.trackingNumbers;
       }
      
       public void setTrackingNumbers(List<TrackingNumber> trackingNumbers)
       {
       this.trackingNumbers = trackingNumbers;
       }
      }
      


      The EJB-QL that caused the problem is:

      ..........
       String query = "select o from Order o " +
       "left join fetch o.addresses a " +
       "left join fetch o.orderItems ol " +
       "where o.id = :orderId";
      
       Query q = this.em.createQuery(query);
       q.setParameter("orderId", orderId);
      ..........
      


      Why can't I "left join fetch" preload more than one of these collections at the same time? I was originally eagerly-loading the addresses but I decided that this was bad due to how my application uses this data...it would deteriorate performance at a level I'm not happy with.

      Is this a bug or a limitation?

      Thanks!