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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Parent child object problem" Watch "Parent child object problem" New topic
Author

Parent child object problem

Tim Resh
Greenhorn

Joined: Aug 09, 2006
Posts: 19
I have the following code base which I inherited in my current job. fun, Fun , Fun

Persistent, Zoo, Animal are objects

Hospital is a helper class

If you look at Animal you see a instance variable called whichZoo and is of type Zoo

If you look at Class Zoo you see a method call addAnimal(Animal) this sets whichZoo in animal to this and adds to the Zoo
treemap.

Here is the issue look at the Hospital.isSick(animal) method, the first thing it does is create a
Zoo object which loads a all zoos ( yes I mean all) which in turns loads all animals in zoos

this was a crazy loop that was solved via a Utility.loadZoo(int zooId) method that tested the
zooId against the database zooId.

if (db.getZooId()== zooId) { return Zoo }

this was in a while loop going through each zoo in the database.

The whole point was to get the reference to the Zoo from the Animal in a parent child like event.

This allowed all Utility methods to do a Zoo zoo = animal.getwhichZoo();
and be able to use all methods in the Zoo object, only by the if statement does it happen to work but objects in memory
are over burdened and some third party reporting tools loops through object lists which eventually
blows the heap.

Is there a way to get e reference to the parent object and cast it, refactoring is not an option because everything reference Zoo zoo
even when it is only using a Zoo.[method] call but there are cases where it is valid.
There is 100's of these in the programs.

Anyone?



Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

Your example references the San Diego Zoo, one of my favorite places, so I'd love to help. I'm having trouble parsing your question though. In particular:

"only by the if statement does it happen to work but objects in memory are over burdened"

I don't get that at all. Then you ask if it's possible to get a reference to the parent object. If you're asking if it's possible to navigate a one-way relationship in reverse, then pretty much no, you can't do that. You could refactor to make it a two-way relationship. You say refactoring is impossible, but, again, I can't really understand your reasoning.
Tim Resh
Greenhorn

Joined: Aug 09, 2006
Posts: 19
You have it conceptually my issue. The Zoo has a list of animals, each animal has a class variable which is of type Zoo. When the animal if added to the Zoo the whichZoo(this) is set prior to the animal.put. The result is every animal in the current Zoo is loaded into the animal.whichzoo which has all Zoo and then has a list of all animals which has a whichzoo (Zoo object) ad nauseum.



The problem is that every method that has a Animal object passed to does the following code. note that line 1 of the method is all over the place in different methods.



The whole idea was to be able to use the animal to get zoo information. animal.zoo.getSomeMethodInZooClass().

Yes a two way object, I believe is what we need. The refactoring I believe would be difficult since the verbiage if Zoo or zoo whther it come from the Zoo Class or the animal.getwhichZoo method which returns a Zoo class.

over 200 methods in 20+ classes either helper class or instance class.

If I was to create a two way object, how do you suggest the linkage between animal and zoo?


Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2854
    
  11

Ah, I was mistaken then. There already is a two-way relationship between Animal and Zoo. Other than a few extra characters, there's no difference between animal.zoo.getSomeMethodInZooClass() that you want to do, and animal.getWhichZoo().getSomeMethodInZooClass(), which you can do. Well, there is an extra method call, but that's not a big deal.

This kind of circular reference really isn't a problem. At least it doesn't suffer from the "ad nauseum" effect as you seem to be defining it.

I do have a couple of suggestions though. A TreeMap is used for sorted collections, and here it's only sorted by the order Animals are added to the Zoo. You're paying a price in performance for no real reason. I'd switch that out for an ArrayList, or possibly a HashMap keyed on the animals description and/or id. Also when you add an animal to a zoo, you should check to see if it's already in a zoo, and either reject the add or remove it from the other zoo's collection. You may want to synchronize the collection or the methods that modify it in order to make the classes thread safe.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Parent child object problem