So I have a chain of objects that reference each other from ORM / Hibernate
- Continent
- has many countries
- has many states
- has many cities
- has many cityparts
- Country
- has one Country
- has many states
- has many cities
- has many cityparts
- State
- has one Continent
- has one Country
- has many cities
- has many cityparts
- City
- has one Continent
- has one Country
- has one State
- has many cityparts
- City part
- has one Continent
- has one Country
- has one State
- has one City
For some methods i need to be able to do some quick lookups for identifying keys.
So for that I currently have in all the objects that have Many to be able to do quick lookups of the Id's of the object in memory without having to fire off a gazillion database queries in the lifetime of the execution thread
HashMap<Integer,Country> countriesTable
HashMap<Integer,State> statesTable
HashMap<Integer,City> citiesTable
HashMap<Integer,CityPart> citypartsTable
But what i'm worried about is cyclic references being held and the objects never getting cleared by garbage collection because they all reference eachother through the ORM relations and they all have private maps pointing to eachohter
Now I have read about WeakHashMap that it will clear the references if there are no more strong references.
The question is Is it adviceable to use WeakHashMap in my situation or am I enough served by Hashmap and this setup and cyclic references will not impair my memory management.