Create Domain Model

Example code is available on github.com/vaadin-on-grails3/gorm-domain.

Open and study Grails documentation how to work with domain classes. In case you are new to Grails domain classes, you should definitelly read it.

We are going to create application that creates a domain object. Then the domain objects are used by 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.

Step 4

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

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

Records from database displayed in browser

Last updated

Was this helpful?