-->
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.  [ 3 posts ] 
Author Message
 Post subject: eagerly loading a many-to-one property in native SQL
PostPosted: Mon Jul 25, 2005 12:17 pm 
Newbie

Joined: Thu Apr 28, 2005 8:16 pm
Posts: 9
I eagerly loading a many-to-one property in native SQL.

My current solution involves adding a return-join element to my query declaration for the many-to-one property I am eagerly loading. A consequence of my current approach is that my result set is returned as an array. Please see below.

I don't need the result set to be returned as an array, since I can traverse to the eagerly loaded many-to-one property (result-array[1]) from the first element of the array (result-array[0]), but I do want to eagerly load the many-to-one property. Is there a way to eagerly load a many-to-one property in native SQL without including multiple return... elements in the query declaration?

Here is the beginning of the query declaration (note the return-join element for the many-to-one property I am eagerly loading):

Code:
<sql-query name="course.selectBySearchString_withJoin">
    <return alias="course" class="Course"/>
    <return-join alias="smdp_cat_code" property="course.courseCatCode"/>
    select
        smdp_assbch_course.rowid {course.courseId},
        smdp_assbch_course.course_number {course.courseNumber},
        smdp_assbch_course.title {course.title},
        smdp_assbch_course.hours {course.hours},
        smdp_assbch_course.cat_code_id {course.courseCatCode},
        {smdp_cat_code.*}
    from
        smdp_assbch_course,
        smdp_cat_code
    where
...


Here is how I use the query:

Code:
...
// fetch courses from the database
List courses_withJoin = HibernateUtil.getSession().getNamedQuery("course.selectBySearchString_withJoin")
.setLong("school_id", school.getSchoolId().longValue())
.setString("degree_type_code", template.getDegree().getDegreeTypeCode())
.setString("search_string", searchString)
.list();
           
// since there is a declared join in the SQL query, Hibernate is going
// to return a list of arrays, where the course is the first element
// in each array; there has got to be a better way to do this
List courses = new ArrayList();
for(Iterator i0 = courses_withJoin.iterator(); i0.hasNext(); ) {
    courses.add(((Object[])i0.next())[0]);
}
   
// place the courses in the request for the JSP
request.setAttribute("courseCount", new Integer(courses.size()));
request.setAttribute("courses", courses);
...


Thank you in advance for any advice on this subject.

Geoff


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2005 1:11 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
The result will always be an array. You should be easily able to do object = array[0];

;)


Top
 Profile  
 
 Post subject: Why, though..?
PostPosted: Fri Aug 12, 2005 10:03 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2003 10:29 pm
Posts: 27
I'm having the same issue. Why does it need to return an array? If you've declared that the joined table is a property of a root object, you've given it enough information to know that the object will already be returned as a property of said root object, and needs not be returned as an explicit object.


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