-->
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.  [ 1 post ] 
Author Message
 Post subject: Simple Filtering of Child Objects
PostPosted: Fri Sep 22, 2006 9:08 pm 
Newbie

Joined: Fri Sep 22, 2006 3:24 pm
Posts: 2
My basic situation is that I have an object named Committee, and the Committee has members that belong to it (the data structure is a little more complex, but this covers the principle).

There is a method Committee.getMembers() which returns members.

At a certain point I want Hibernate to bring back a list of Committee objects with a list of members, where the members have been sorted first by the Committee name and then by the member name.

So, ideally, I run a query for the committees and join the members ...
Code:
Collection list = getSession()     
      .createCriteria(Committee.class)
      .createAlias("members", "m")
      .addOrder(Order.asc("this.name"))
      .addOrder(Order.asc("m.lastName"))
      .addOrder(Order.asc("m.firstName"))
      .list();


Then later I have a jsp page using jstl tags (again not exact code, has been simplified)

Code:
<c:forEach items="${results}" var="committee">
   <c:out value="${committee.name}:" />
   <c:forEach items="${committee.members}" var="m">
      <span style="style="margin-left: 50px">
      <c:out value="${m.firstName} ${m.lastName}" />
      </span>
   </c:forEach>
</c:forEach>


Which would output a list where the Committees are in order by name, and the Members are indented under the committees and also ordered by name.

What I have just stated above does not work however, and would actually create one committee listing for every member under that committee (just like a sql join, it duplicates the committees). Furthermore, the children are not actually ordered, since it lazily requeries more data at time of page compilation (it executes committee.getChildren() independantly)

I would like an easy, comprehensive way to be able to run a query ahead of time and have Hibernate keep the query information and assign the appropriate child data to the parent object.

So far I have figured out two ways to do this, neither of which are ideal.

1. Insert .setResultTransformer(Criteria.ALIAS_TO_ENTITY_MAP) and return a list of Hashmaps. Run a post-query section of code to keep adding child objects to the parent while the parent's ID stays the same. When a new ID is encountered, save that parent to the results list and create a new parent to store the next set of data to. This is annoying to code and does not make it easy to edit the query contents later on.

2. Query a set of Committees, then apply getSession().createFilter(committee.getMembers(), "order by this.lastName, this.firstName") to every committee and store the results. The code for this is easier to create but it separates the query criteria over a range of code and at execution this runs twice as slow. This is from a 3 second loading time to a 6 second loading time, which I think is significant.

Does anyone have any better idea how to go about this or know of some way Hibernate can simplify this type of task? I don't want to add order statements or turn off lazy loading in the xml files, and need to be able to do this from the query or simple post-query logic.

Help is much appreciated. This seemed so basic to me at first but has become quite difficult to figure out.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.