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
  • Step 1
  • Step 2
  • Step 3
  • Step 4

Was this helpful?

  1. Database
  2. GORM

Create Domain Model

PreviousGORMNextTransactions

Last updated 5 years ago

Was this helpful?

Example code is available on .

Grails documentation has nice description how to work with classes. In case you are new to Grails domain classes, you should definitelly read it.

Anyway, let's show an example how to create a domain object and use it in Vaadin application.

Step 1

Run Grails create-domain command that will create the new domain class Item.

grails create-domain-class com.app.Item

The generated and empty domain class Item.groovy is in grails-app/domain/com/app folder.

Step 2

Before we start testing the domain class, we need to add there at least one field, for example, a string label.

package com.app

class Item {

    String label

    static constraints = {
    }
}

Domain object must be stored in grails-app/domain, contain at static field constraints to be considered as a valid domain object. Only then a database table for a domain object will be created.

Step 3

Now, we need to create few records of Item in the database during the application start-up.

Open BootStrap.groovy and save few Item instances there.

import com.app.Item

class BootStrap {

    def init = { servletContext ->
        new Item(label: 'First').save()
        new Item(label: 'Second').save()
        new Item(label: 'Third').save()
        new Item(label: 'Fourth').save()
    }

    def destroy = {
    }
}

Step 4

Use domain class to fetch all records from the database and display them in the browser.

import com.app.Item
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.Label
import com.vaadin.ui.UI
import com.vaadin.ui.VerticalLayout

class MyUI extends UI {

    @Override
    protected void init(VaadinRequest vaadinRequest) {

        VerticalLayout layout = new VerticalLayout()
        layout.setMargin(true)

        List<Item> items = Item.findAll()
        for (Item item : items) {
            String label = item.label
            layout.addComponent(new Label(label))
        }

        setContent(layout)
    }
}

Run the application and database records will be displayed in UI.

Records from database displayed in browser
github.com/vaadin-on-grails/gorm-domain
domain