Vaadin 7 on Grails 2.x
  • Introduction
  • Project setup
    • Command Line
    • IntelliJ IDEA
    • Eclipse
    • NetBeans
    • Plugin Configuration
    • Clean up
    • Best Practices
  • Database
    • GORM
      • Create Domain Model
      • Transactions
      • LazyInitializationException
      • Open Session In View I.
      • Open Session In View II.
      • Table Container
    • Groovy SQL
      • Create Sql
      • Execute SQLs
    • MyBatis
      • Configuration
      • Basics
    • JdbcTemplate
      • Create Beans
      • Usage
    • Clean Up With Alternatives
  • Architecture
    • Model View Presenter
  • Spring Autowiring
    • Create Simple Application
    • Application With Navigator
  • UI
    • Re-using GORM Validations
    • Async Push
    • Multiple application
    • SASS Compilation
    • Widgetset Compilation
  • Spring Security
    • Spring Security Dependency
    • Spring Security Basics
    • Secured Navigator
  • Localization
    • Localization Basics
    • Custom MessageSource
  • REST
    • Without using root URL
    • Using root URL for Vaadin app
  • Plugin development
    • Github
    • Development
Powered by GitBook
On this page
  • The Issue
  • How to prevent LazyInitializationException

Was this helpful?

  1. Database
  2. GORM

LazyInitializationException

PreviousTransactionsNextOpen Session In View I.

Last updated 5 years ago

Was this helpful?

Example code is available on .

When we create a domain model that will cause lazy loading of other domain object, we might run into the troubles with LazyInitializationException.

The Issue

In order to fully understand, lets show the code simulates LazyInitializationException.

Create Account class that will just contain name property.

class Account {

    String name

    static constraints = {
    }
}

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.

class User {

    String username
    String password

    static belongsTo = [account: Account]

    static constraints = {
    }

    public String getLabel() {
        return "$username - $account.name"
    }
}

In order to simulate the issue, here is the Vaadin code that will create a ComboBox with an instance of User class.

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)

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.

static mapping = {
    account lazy: false
}

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.

github.com/vaadin-on-grails/gorm-LazyInitializationException