-->
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.  [ 1 post ] 
Author Message
 Post subject: How to join fetch multiple table properly?
PostPosted: Thu Jul 05, 2007 5:32 pm 
Newbie

Joined: Thu Jul 05, 2007 4:58 pm
Posts: 4
Hibernate version:3.2.4.sp1

Name and version of the database you are using:MySQL, version: 5.0.38-Ubuntu_0ubuntu1-log

Mapping documents:
Code:
@Entity
@Table(name = "IN_Products", uniqueConstraints = @UniqueConstraint(columnNames = "itemNo"))
public class InProducts implements java.io.Serializable {
   @Id
   @GeneratedValue
   private int pid;

   @Column(name = "itemNo", unique = true, nullable = false, length = 60)
   private String itemNo;

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "inProducts")
   private Set<WebProductSpec> webProductSpecs = new HashSet<WebProductSpec>(0);

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "inProducts")
   private Set<WebProductDesc> webProductDescs = new HashSet<WebProductDesc>(0);
...
}


Code:
@Entity
@Table(name = "WEB_ProductSpec")
public class WebProductSpec implements java.io.Serializable {

   private WebProductSpecId id;

   private InProducts inProducts;

   public WebProductSpec() {
   }

   public WebProductSpec(WebProductSpecId id, InProducts inProducts) {
      this.id = id;
      this.inProducts = inProducts;
   }

   @EmbeddedId
   @AttributeOverrides( {
         @AttributeOverride(name = "pid", column = @Column(name = "pid", nullable = false)),
         @AttributeOverride(name = "url", column = @Column(name = "url", nullable = false, length = 256)) })
   public WebProductSpecId getId() {
      return this.id;
   }

   public void setId(WebProductSpecId id) {
      this.id = id;
   }

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "pid", nullable = false, insertable = false, updatable = false)
   public InProducts getInProducts() {
      return this.inProducts;
   }

   public void setInProducts(InProducts inProducts) {
      this.inProducts = inProducts;
   }

}


Code:
@Entity
@Table(name = "WEB_ProductDesc")
public class WebProductDesc implements java.io.Serializable {

   private WebProductDescId id;

   private InProducts inProducts;

   private String description;

   public WebProductDesc() {
   }

   public WebProductDesc(WebProductDescId id, InProducts inProducts,
         String description) {
      this.id = id;
      this.inProducts = inProducts;
      this.description = description;
   }

   @EmbeddedId
   @AttributeOverrides( {
         @AttributeOverride(name = "pid", column = @Column(name = "pid", nullable = false)),
         @AttributeOverride(name = "locale", column = @Column(name = "locale", nullable = false, length = 5)) })
   public WebProductDescId getId() {
      return this.id;
   }

   public void setId(WebProductDescId id) {
      this.id = id;
   }

   @ManyToOne(fetch = FetchType.LAZY)
   @JoinColumn(name = "pid", nullable = false, insertable = false, updatable = false)
   public InProducts getInProducts() {
      return this.inProducts;
   }

   public void setInProducts(InProducts inProducts) {
      this.inProducts = inProducts;
   }

   @Column(name = "description", nullable = false)
   public String getDescription() {
      return this.description;
   }

   public void setDescription(String description) {
      this.description = description;
   }

}


How would I populate the WEB_ProductDesc and WEB_ProductSpec of the IN_Product object with a single call to SQL, without resulting in huge cartesian product? Also, I need the result to be display in pages.

I've tried the following but it seems to ignore the fetch mode and dynamically fetch it whenever the data is accessed. The good thing is pagination seems to work properly.

Code:
Criteria criteria = session.createCriteria(InProducts.class);
   criteria.addOrder(Order.asc("itemNo"));
   criteria.createAlias("webProductSpecs", "s").setFetchMode("s", FetchMode.JOIN);
   criteria.createAlias("webProductDescs", "d").setFetchMode("d", FetchMode.JOIN);
   criteria.setMaxResults(10);
List<InProducts> p = criteria.list();


Where as this one fetches everything but pagination seems to apply in memory, which is not a good thing.
Code:
Query q = em .createQuery("FROM InProducts i INNER JOIN FETCH i.webProductSpecs INNER JOIN FETCH i.webProductDescs");
q.setMaxResults(10);
List<InProducts> p = q.getResultList();

I get WARNING: firstResult/maxResults specified with collection fetch; applying in memory!

Any thoughts?

I also found it's possible to do this fetch efficiently with NHibernate's CreateMultiQuery() as illustrated at http://ayende.com/Blog/archive/2007/06/20/Efficently-loading-deep-object-graphs.aspx so I'm hoping there's a solution for using Hibernate's Criteria or HQL. Otherwise I guess combining the tables into a single table would be the most efficient way.


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

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.