-->
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.  [ 6 posts ] 
Author Message
 Post subject: Batch fetching: how to retrieve objects with collections
PostPosted: Mon Jul 17, 2006 7:18 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 5:15 am
Posts: 21
Hi,

I'm trying to fetch a list of objects where each root object has its own collections. Since there will be hundreds of thousands of instances I'd need to read them iterating over each one by one or with some kind of pagination. So far, I've been unable to do this.

The problem is that ScrollableResults seems to read rows, not objects. Let's say you have a "Parent" object with a collection of three "Child" objects. You will get after the joins three rows. Each call to ScrollableResults.next() retrieves the next row that means that you will have three parents with a child each.

Even using a .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) in the criteria doesn't solve this.

Another way would be to set the first result and the number of results like:

Code:
         .setFirstResult(firstRow)
         .setMaxResults(numberOfRows)


But this also doesn't work. Again, it's row oriented, not object oriented.

How are you handling this ? Any comment about this would be really welcome.

Regards,

- Juan


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 18, 2006 10:38 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 5:15 am
Posts: 21
I've been trying several combinations without success. As far as I can see Hibernate relies completly on the rows of the resultset returned by the JDBC driver.

Just to know if I should continue investing more time on this, does anyone know if, at least, is possible to retrieve objects with collections using a batch approach ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 3:37 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
The Query interface has an iterate() method. It populates a root entity when hasNext() is called. So, if your query sets FetchMode to join I think you'll get the batch in question and only one parent object.

I've had success loading lazy collections like this:
Code:
List ships = 
          getHibernateTemplate().getSessionFactory().getCurrentSession()
          .createCriteria(Shiptodepartment.class)
          .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
            .setFetchMode("billtoshiptos", FetchMode.JOIN)
                        ...
           .add(Expression.eq("shiptodeptid", l))
         .list();



This provides me 1 root entity and * the associated collection I tell it to get (via setFetchMode). You can also create an alias and get collections of the setFetchMode collection instance - if that makes any scence at all :-).

e.g. objectA has many objectB's and objectB has many objectC's. You can use objectA to get objectsB's and use the alias & setFetchMode on objectB to get it's objectC's.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 9:42 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 5:15 am
Posts: 21
How are you using iterate() ? You can't set the fetch to "join" in the Query and then iterate.

In the docs (http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#queryhql) is clearly stated that: Note that the fetch construct may not be used in queries called using iterate() (though scroll() can be used).


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 19, 2006 10:25 am 
Regular
Regular

Joined: Sat Nov 06, 2004 5:20 pm
Posts: 54
Location: Collierville, TN
Sorry my friend...,

partA where I mention iterate() is not concrete - meaning I haven't implemented it. I was just trying to brainstorm with you. You'll notice the words "if" and "I think".

You did the right thing by looking further into the docs to see if it's doable.

PartB where I have an actual example is concrete and works.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 20, 2006 5:21 am 
Beginner
Beginner

Joined: Thu Jun 30, 2005 5:15 am
Posts: 21
Thank you for your comments anyway.

So far, I think this is a limitation in Hibernate. I'm giving up on this.


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