-->
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.  [ 8 posts ] 
Author Message
 Post subject: Howto Limit the number of returned objects defined by <se
PostPosted: Mon Sep 01, 2003 11:05 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
Suppose ClassA may contain zero or more ClassB

Code:
<set name="ClassB" table="ClassB">
  <key>
    <column name="key"/>
  </key>         
  <one-to-many class="foo.ClassB"/>
</set>


ClassA may contain a lot of ClassB , and I only want to load the latest ClassB ,
or sort ClassB to get prior N objects. (ClassB implements Comparable)
I looked up the manual , it seems no way to limit the number of returned ClassB.

I know HQL can limit the number of returned rowsets by Query.setMaxResults(int maxResults)
But in this case , ClassA.getClassBSet() is automatically loaded by hibernate , how does HQL step in ?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2003 11:28 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is a FAQ.


Use a collection filter.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 01, 2003 11:50 pm 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
gavin wrote:
This is a FAQ.


Use a collection filter.


Hi , but this will make my domain object dependent of Hibernate ...

Code:
public class ClassA implements Serializable
{
  private Set ClassBSet;
  ...
  public Set getClassBSet()
  {
    return ClassBSet;
  }
  public void setClassBSet(Set s)
  {
    this.ClassBSet = s;
  }
}

Is it possible to make my domain object as 'clean' as this ?

I don't want to add hibernate-dependent code in get/setClassBSet(), I hope this is automatically set by hibernate , it it possible ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 2:57 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
Hi , this is how I make use of collection filter :
Code:
ClassA classA = (ClassA) session.load(ClassA.class , key);
System.out.println("Original ClassBSet size = " + classA.getClassBSet().size());
Collection c = session.filter(classA.getClassBSet(),"where ...");
classA.setClassBSet(new TreeSet(c));
System.out.println("After filtering, ClassBSet size = " + classA.getClassBSet().size());


It really filters out what I want , but ...I have one question .
In the first println() , hibernate still gets all ClassB's instances ,
meaning that upon session.load(ClassA,class , key) , hibernate still loads all ClassB.
I am afread that if there are a lot of ClassB , it will out of memory...
Is it possible to set filter before load ClassA ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 3:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, obviously you shouldn't do the first println!!!


LOL.


it is the toString() method that is causing the lazy collection to be initialized.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 3:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
actually the call to size()


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 3:21 am 
Beginner
Beginner

Joined: Wed Aug 27, 2003 3:46 am
Posts: 34
Location: Taiwan
So , if ... if I don't do the first println() , hibernate will not load all ClassB ?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 02, 2003 3:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
As long as you map the collection with lazy="true"


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