org.hibernate.LazyInitializationException: How to properly use Hibernate's lazy loading feature

前端 未结 3 1019
名媛妹妹
名媛妹妹 2020-12-05 16:27

I got some trouble loading a list of objects from my database using Hibernate and lazy=true mode. Hope that someone can help me out here.

I have a simple class here

3条回答
  •  感动是毒
    2020-12-05 17:00

    The reason you're getting the exception might be that the transaction you load the data in is closed (and the session with it), i.e. you're working outside the session. Lazy loading is especially useful when working with entities in one session (or across sessions when correctly employing a second level cache).

    AFAIK you can tell Hibernate to automatically open a new session for lazy loading but I didn't use that for a while and thus I'd have to look up how that works again.

提交回复
热议问题