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?
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
how do I do my own kindle-like thing - without amazon