aspose file tools*
The moose likes Java in General and the fly likes Improving An Algorithm Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Improving An Algorithm" Watch "Improving An Algorithm" New topic
Author

Improving An Algorithm

Jason Ferguson
Greenhorn

Joined: Jul 17, 2007
Posts: 9
I'm looking for a Better Way (tm) than a method I'm using.

This algorithm is in a Hibernate ResultTransformer implementation, but I don't think this question is Hibernate specific so I'm asking it here.

The query is for an Organization, with some basic fields such as id, name, etc. However, the Organization also contains a Collection of child Organizations, which seriously impacts performance when building the hierarchy recursively (especially considering Hibernate many-to-one/one-to-many relationships with Organization).

In order to improve performance, I implemented the Nested Set model, where each item has a "left" and "right" value. To get all of the children of an organization, I simply query for results between its own left and right values.

However, I'm thinking the method I'm using to rebuild the hierarchy could stand improvement, and am open to suggestions.

Upon the initial return of the query results, I store everything in a Map<String, Object>. This is converted to a List<Map><String, Object>> by Hibernate.

I then create two more maps:
Map<Organization, Integer> childParentMap - maps the organization to the id of its parent.
Map<Integer, Organzation> organizationIdToObjectMap - maps the unique id of the object to the object.

Using these two maps, I can get the id of the parent from the childParentMap, then turn around and get the parent object itself from the organizationIdToObjectMap. Unfortunately, this requires an initial pass through the List<Map><String, Object>> to build the organizationIdToObjectMap and the childParentMap, then a second pass through the organizationIdToObjectMap to build the hierarchy.

The code involved is below.



My gut feeling is that there has to be a more efficient way to do this. Any ideas?

Jason
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Improving An Algorithm
 
Similar Threads
Cascading update problem
Problem in inserting a foreign key
2nd Level cache eviction not cascading
Parent-Child Mapping with Hibernate 2.0
Hibernate self referential interigity