2

I know a proxy object is :

Proxies are the mechanism that allows Hibernate to break up the interconnected cloud of objects in the database into smaller chunks, that can easily fit in memory.

  • What I am trying to figure out is, how does hibernate do it behind the scenes and how is it implemented by hibernate?
  • I mean how is it cached in the memory ? Is it a first level cache or second level cache ? Is there any good read for this ?
2
  • 3
    Someone who downvoted the question , must have some idea, right ? Why not share it ? Commented Jan 12, 2016 at 20:28
  • 1
    Probably because they expect you to use the source - which I found by typing "hibernate github", into Google, as I assumed that's where Hibernate source is found. I was right :) Commented Jan 12, 2016 at 21:19

1 Answer 1

4

Hibernate use javassist to create dynamic proxies instead of concrete entities to populate fields of fetched entity that are referencing other persistent entity (or collection of persistent entities).

(note that if you mark the relationship as eagerly fetched : hibernate won't create proxies but concrete entities. This is NOT the default)

A major advantage of javassist over standard dynamic proxy mechanism is that it allows creation of dynamic proxy on concrete classes, not only interfaces.

The responsability of a proxy is to perform "transparently" a database read operation when required (i.e. when access to a proxied entity is required)

Proxies and first or second level cache aren't really linked concepts. We can just say that if you try to "resolve" a proxy when the entity holding it isn't attached to an open session (i.e. when the entity holding it isn't in the first level cache) it will raise a LazyInitializationException (simply because there is no way perform a database read in this situation)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.