-->
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.  [ 2 posts ] 
Author Message
 Post subject: Inefficient ???
PostPosted: Mon May 09, 2005 2:58 am 
Beginner
Beginner

Joined: Mon Sep 06, 2004 9:36 am
Posts: 35
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.17

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

I have a class

public class TimeAttendance implements Serializable {
private Long timeAttendanceId;
private Employee employee;
private ApprovalStatus approvalStatus;
private Date startTime;
private Date endTime;
...
}

Employee and ApprovalStatus are also classes implementing Serializable.

I want to retrieve TimeAttendance(s) for a particular employee.

public List getTimeAttendances( Long employeeId ) {
List timeAttendances = new ArrayList();

// hibernate query
String hQuery = "from TimeSheet ts " +
"where ts.employee.employeeId = :eid ";

try {
Session session = HibernateHelper.getSession();

Query query = session.createQuery( hQuery );

query.setLong( "eid", employeeId.longValue() );

timeAttendances = query.list();

} catch (InfrastructureException ie) {
System.out.println("TimeAttendanceDAOHibernate.java: ie.getMessage()= " + ie.getMessage());
return timeSheets;
} catch (HibernateException he) {
System.out.println("TimeAttendanceDAOHibernate.java: he.getMessage()= " + he.getMessage());
return timeSheets;
}

return timeSheets;
}


after calling this function as "getTimeAttendances( new Long("77") )",
i expect Hibernate to execute 1 SELECT statement:
select * from time_attendance where employee_id = 77

Hibernate does it but then it start executing select statement for each of the member of the TimeAttendance class which in itself is a class. this is very inefficient as for TimeAttendance class, 8 of its members are also classes.

is there any way to control it? thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 09, 2005 10:39 am 
Senior
Senior

Joined: Tue Feb 08, 2005 5:26 pm
Posts: 157
Location: Montréal, Québec - Canada
You can try to set the variable max_fetch_depth = (1 or 2) on your hibernate.cfg.xml file:

<property name="max_fetch_depth">1</property>

Also, on your mapping file, put the value outer-join=true on the associated entity that you would like to see loaded by an outer join.

<many-to-one class="ApprovalStatus" outer-join=true

_________________
Vincent Giguère
J2EE Developer


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