Hibernate's Lazy strategy(1)

来源:互联网 发布:网络兼职正规 编辑:程序博客网 时间:2024/05/18 16:38
Lazy strategy is the default strategy for fetching entities and collections in Hibernate, and it is tightly related to Proxy.

For Example: User user = (User) session.load(User.class, id);

Then, the got user is actually not a USER, but a proxy acting like a user. Proxy is just a placeholder. When some properties(exception id) called for user,  the proxy is initialized to a actual user(Here a execution of the SQL triggered).

However, If a get() method is used instead, then a DB hit always occurs.  Then no proxy is used. 

But, it is wrong to say Lazy strategy is not used for get.

If user has a collection(ex, friends), then the collection will also be lazy loaded.

TBD.
原创粉丝点击