-->
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.  [ 5 posts ] 
Author Message
 Post subject: Which is better for performance and makes more sense?
PostPosted: Thu Apr 08, 2004 11:03 am 
Beginner
Beginner

Joined: Fri Mar 05, 2004 1:48 pm
Posts: 28
Here is my scenerio.
I have two tables. A STUDENT table and a table of COURSES.
It is a One-To-Many relationship a Student has many Courses.

Lets say I need to find Courses for a Student taken in Fall 2003.
I have two options:

#1
I can get the set/iteration of ALL of the student's courses and loop through selecting the particular OBJECTS which have FALL and 2003 field values.

OR

#2
I can have hibernate select ONLY the records with FALL and 2003 from the start.

It seems to me option# 2 is the better approach. But what if later in the application I need the Courses for Spring 2004? Then I have to query again for those courses instead of looping through the whole set I would have already had from option# 1. Also note, something I do is create a vector of all of the Courses returned by Hibernate. Is this pointless? I don't have a good grasp of everything Hibernate does between the JDBC calls and the objects. Hibernate generated a method called getCourses() which returns the complete set of courses. When I decided to put the Courses into a vector I was making the assumption that each time I call getCourses() it will query the database for them. I was trying to eliminate DB calls. Is this correct? If it is correct, is it more of a performance hit to deal with Java objects in the vector or to make more DB calls?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 10:22 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Do a proper test will give you the answer, there is no absolute answer.
An intermediary solution would be to use a cached query.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 10:35 am 
Beginner
Beginner

Joined: Fri Mar 05, 2004 1:48 pm
Posts: 28
One problem was that I didn't understand all of the behaviors of Hibernate. I still don't, but one thing that is helping is setting hibernate to print out the SQL. I am finding out that it doesn't generate a SQL call as often as I would have thought. My goal is to reduce the DB hits because databse performance is where our organization suffers. Our web apps have to compete with Cobol batch jobs which have priority on MVS :-(


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 20, 2004 10:55 am 
Newbie

Joined: Tue Apr 20, 2004 9:13 am
Posts: 2
Location: Mauritius
Good OOP design would dictate adding the following method to your Student class.

Code:
public Collection getCourseForPeriod(Semester semester) {
   ArrayList courses = new ArrayList();
   Iterator itr = getCourses().iterator();
   while (itr.hasNext() ) {
      Course course = (Course) itr.next();
      if (course.getSemester().equals(semester) ) {
         result.add(course);
      }
   }
   return courses;
}

But, you may prefer to execute a SQL directly if performance becomes an issue.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 5:18 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
So choose the first one and beware stale data.
You'll minimize SQL roundtrips and use more Java CPU

_________________
Emmanuel


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