Skip to content
Preptima
hardDesignConceptMidSeniorStaff

Your lazy-loading proxy works until someone calls equals or getClass on it. What breaks, and how do you design a proxy that survives it?

A lazy-loading proxy is a different object of a different class from its subject, so getClass comparisons, direct field reads and identity checks all see the proxy. A proxy pattern that survives defines equality by an identity key both sides answer without loading, compared through instanceof and accessors.

5 min readUpdated 2026-10-09Target archetype: Enterprise Captive

Answer

Short answer

A lazy proxy is not the subject. It is another object, of another class, that delegates to the subject once loaded. So getClass() comparisons fail, direct field reads on the proxy see empty fields, and == is false. A proxy pattern that survives defines equality on the interface by a key the proxy already holds, and compares through instanceof and accessor methods, never through classes or fields.

Was this answer helpful?

Likely follow-ups

  • Your entity's id is assigned by the database on insert. What does that do to an id-based hashCode, and what do you return instead?
  • A debugger shows the proxy's toString and the session is already closed. What happens, and how should toString behave?
  • Two threads touch an uninitialised proxy at the same moment. How many times does the loader run in your design?
  • Code elsewhere switches on the concrete class of a document. How do you migrate it once proxies are introduced?

Related questions

Further reading

Related Topics