Now create class User that will belong to Account and in toString() method, just try to get value from account field. When that code is executed, Hibernate will try to load an account from database and will throw LazyInitializationException.
In order to simulate the issue, here is the Vaadin code that will create a ComboBox with an instance of User class.
How to prevent LazyInitializationException
The solution to avoid LazyInitializationException, is to disable lazy loading for class that should be also fetched at once with the other domain object.
Say to Hibernate to load account together with the user. Add the following code into User class.
When you try to execute the application, you the account will be already loaded in getLabel() method and it will not fail.
This also makes sense from performance point of view, because we will avoid multiple database calls and the user and account will be loaded together, in single database call.
Another approach is to have open session for each request. Read the next two articles about Open Session In View to find out how to implement it in your application.
VerticalLayout layout = new VerticalLayout()
ComboBox userAccount = new ComboBox()
User user = User.findAll()[0]
BeanItem item = new BeanItem(user, 'label')
userAccount.addItem(item)
layout.addComponent(userAccount)
setContent(layout)