Why is hibernate returning a proxy object?

后端 未结 3 1091
时光取名叫无心
时光取名叫无心 2021-02-02 03:09

I have a service method that calls a DAO which then returns an object from the database. This method is called from numerous parts of the system. However, one particular method

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 03:53

    Hibernate returns proxies if not all members are resolved, i.e. the object is not complete. This is often a desired feature to improve performance and is (I think) the default setting in hibernate.

    If you do not want proxies you can suppress lazy loading in the hbm.xml file, i.e. use eager loading. Please check the hibernate docs for the exact syntax.

    To use the proxy object just never access a member directly but only via getter, even within member functions. Hibernate magic fills in the member when you get it. This way you never have to expose the object. Also don't use instanceof on potential proxy objects. But this is a code smell anyway.

提交回复
热议问题