-->
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: List efficiency
PostPosted: Tue Feb 14, 2006 8:32 pm 
Newbie

Joined: Tue Feb 14, 2006 7:54 pm
Posts: 2
I'm tring to write simple BBS system that has a few Forums, each forum has a number of posts associated to it, and each post could have a number of replies associated to it.

My hibernate mapping file looks like this (simplified):
Code:
    <class name="org.ajiaojr.linnet.db.Forum" table="forum">
        <id name="forumId" type="integer">
            <column name="forum_id" />
            <generator class="native" />
        </id>
        <list name="posts" lazy="true" batch-size="40" order-by="postDate asc">
            <key>
                <column name="forum_id" not-null="true" />
            </key>
            <one-to-many class="org.ajiaojr.linnet.db.Post" />
        </list>
        <list-index column="post_id" />
    </class>
    <class name="org.ajiaojr.linnet.db.Post" table="post">
        <id name="postId" type="integer">
            <column name="post_id" />
            <generator class="native" />
        </id>
        <many-to-one name="rootPost" class="org.ajiaojr.linnet.db.Post" fetch="select" lazy="true">
            <column name="root_post" not-null="true" />
        </many-to-one>
        <list name="replies" inverse="true" lazy="true" batch-size="40">
            <key>
                <column name="root_post" not-null="true" />
            </key>
            <one-to-many class="org.ajiaojr.linnet.db.Post" />
        </list>
    </class>


My question is that assume there are 2000 posts in a forum, it's natural that they should be splited into pages, if I were to access the 1500th - 1540th post, will the posts 1-1499 be fetched? If so, it'll mean a big performance impact, is there a way to avoid fetching those?

Thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 9:01 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Both HQL queries and Criteria support "setFirstResult" and "setMaxResults" for pagination. They're described in the ref docs, too. You can also use ScrollableResults. Read section 10.4.1.* of the ref docs for ideas.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 9:18 pm 
Newbie

Joined: Tue Feb 14, 2006 7:54 pm
Posts: 2
If I were to retrive the Forum object, and access the posts from the forum instance, how should I write the criteria?

For example:

Code:
Forum forum = (Forum) session.get(Forum.class, "someID");
for(int i=0; i<40; i++){
    Post p = forum.posts.get(1500+i);
    System.out.println(p.title);
}


How should I write the criteria to optimize the fetch of the collection of posts inside the forum object?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 10:01 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you know the ids in advance, you don't even have to use setFirstResult/setMaxResults. You can build a Collection of all the ids you want, then use Criteria with Restrictions.in("forumIdProperty", collectionIds);


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.