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

前端 未结 2 1135
你的背包
你的背包 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.

    0 讨论(0)
  • 2021-01-22 11:16

    This exception means that the FileLocations association is not yet loaded and you are trying to access it. There is no session opened, and there is no way to load the association. This is the LazyInitializationException.

    Edit: Read Bozho's post for the solution.

    0 讨论(0)
提交回复
热议问题