load() just returns a proxy by default and database won’t be hit until the proxy is first invoked.
what does it mean by p开发者_开发技巧roxy exactly here ?
First of all, Proxy is a design pattern. In sense of Hibernate, it is dynamically subclassing your object at runtime. The proxy object will contain the same methods as your object(that's why you don't realize that you are dealing with a proxy), and as you say the database won’t be hit until the proxy is first invoked.
To explain further about proxies; they are created dynamically by extending(creating a sub class) of your object at runtime. This subclass has all the methods of the parent entity, and when any of the methods are accessed, the proxy loads up the real object from the DB and calls the method for you. Additionally , it is worth to mention that all the typecasting and the instanceof operator works perfectly fine on the proxy as it is a direct subclass, hence typically you are not required to perform any special operation in realization that you have a proxy instead.
精彩评论