I found the following lines from
Java Persistence with hibernate, topic
13.1.3 Understanding proxies.
My question is that how does hibernate know if there is an entry for Item with Id 123 in database table without actually hitting the database.
Proxies are placeholders that are generated at runtime. Whenever Hibernate
returns an instance of an entity class, it checks whether it can return a proxy
instead and avoid a database hit. A proxy is a placeholder that triggers the loading
of the real object when it’s accessed for the first time:
The third line in this example triggers the execution of the SQL that retrieves an
Item into memory. As long as you access only the database identifier property, no
initialization of the proxy is necessary. (Note that this isn’t true if you map the
identifier property with direct field access; Hibernate then doesn’t even know that
the getId() method exists. If you call it, the proxy has to be initialized.)