Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6573887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:11:25+00:00 2026-05-25T15:11:25+00:00

I am currently rolling my own little ORM, and find myself faced with the

  • 0

I am currently rolling my own little ORM, and find myself faced with the task of creating a canonicalizing mapping in order to prevent loading the same entity from the database more than once.

My current approach is to use a HashMap<Object, WeakReference<Object>>. The key is the primary key of the mapped database-entity (an ArrayList<Object> if it is a composite key), and the values are WeakReference<Object>.

My main problem is how to clean the map up? When an object is not used any more, the weak reference in the map will go null, and I will only discover this on the next lookup (or never, if I don’t look the object up again). I could make the weak references register with a ReferenceQueue when they get cleared, and then check that queue every time I look something up. The cleared reference would not give me any hint as to which object was cleared though, so I guess I would have to subclass WeakReference to store the key in the map, so I can remove it after the reference was cleared.

Is this the way to go, or is there any simpler way to implement this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:11:26+00:00Added an answer on May 25, 2026 at 3:11 pm

    I would recommend using Guava’s MapMaker, or the CacheBuilder in r10.

    They allow automatic* time- and size-based eviction, as well as supporting weak keys or values. (The upcoming CacheBuilder promises to be specially tailored to this kind of use case.)

    So you can initialize your map:

    ConcurrentMap<Key, Object> cache = new MapMaker()
            .weakValues()
            .makeMap();
    

    And the immediate benefit will be that when a value is garbage collected, the whole entry will be removed. Furthermore, you can use a computing map:

    ConcurrentMap<Key, Object> cache = new MapMaker()
            .weakValues()
            .makeComputingMap(loadFunction);
    

    where loadFunction is a Function<Key, Object> that loads an object from the database. The advantage of this is that the map will handle concurrent requests for a particular object, ensuring the query is only called once. Additionally, the requesting code needs only call get() and can always expect the object back, whether from cache or the database.

    These examples are using MapMaker – I haven’t had the pleasure to toy with CacheBuilder yet.

    See my question my ideal cache using guava for more examples. That post discusses how to combine time-based eviction with canonicalization.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently creating a business case for rolling out TFS 2010 as our
I am rolling my own simple web-based perfmon, I am not happy with some
I'm learning more about javascript OOP by rolling my own console.log variant with some
...or am I stuck rolling my own XML chopping functions. I'd like to create
I'm rolling my own ActivatableCollection<T> for db4o but cribbing heavily from the builtin ActivatableList<T>
I’m currently responsible for rolling out the use of jQuery to the community of
Currently we are saving all SOAP requests to a log file (rolling, one per
I'm rolling my own logger class, and want to represent the heirarchy of logs
Currently we use jQuery to add RIA goodness to our apps, but recently we
Currently I have a class that looks like this: public class MyClass : IMyClass

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.