In Spring with jpa/hibernate, how do I keep a session open to avoid lazy initialization exceptions?

前端 未结 6 1593
温柔的废话
温柔的废话 2020-12-02 14:52

I currently mark collections in entity beans as eager to avoid getting a lazy initialization exception when I try to access the collection properties after loading the bean

6条回答
  •  一个人的身影
    2020-12-02 15:12

    One final option which seems to of been missed is you can build your object graph based on your use-case by using a JOIN.

    This will result in the object being initialized, i.e the will not be a proxy.

    Use this approach if you are in control of the client (i.e you not creating open service publishing an api) because you need to know what state is being touched when the session is closed because the transaction has closed.

提交回复
热议问题