LazyInitializationException when adding to a list that is held within a entity class using hibernate and gilead for gwt

前端 未结 2 1136
你的背包
你的背包 2021-01-22 10:46

Right so i am working with hibernate gilead and gwt to persist my data on users and files of a website. my users have a list of file locations. i am using annotations to map my

2条回答
  •  北恋
    北恋 (楼主)
    2021-01-22 11:04

    Lazy means that the values of the collection are loaded from the database only when they are accessed. If at that time the Session has been closed, the LazyInitializationException is thrown, because the data cannot be fetched.

    In your case I'd simply suggest adding an eager fetch type to the association:

    @ManyToMany(cascade = CascadeType.ALL, fetchType=FetchType.EAGER)
    

    This will load the fileLocations when the entity is loaded and no lazy loading will be needed.

    A common solution is to use OpenSessionInView, but it may not always work with GWT, becaucse the client is remote and the session cannot be opened there.

    So you are going to have some problems with lazy initialization. You can search around for related questions, there are a few - this and this for example.

提交回复
热议问题