Vaadin on grails - Database access

Versions used in this tutorial: Grails 2.3.x, Vaadin 7.1.x. News and updates about Vaadin on Grails are available on VaadinOnGrails. This is continuation of Vaadin on Grails - Create project in IntelliJ IDEA

We are going to create persistence a domain class that is automatically mapped into a database through Hibernate. GORM from Grails framework will do most of the automatic stuff there.

Create persistent layer
  1. Right click on the domain folder and select Create Domain IDEA create domain class

  2. Fill in the full class name. If we put there only Author then the package name will be automatically added based on the name of the project. IDEA domain name

  3. Add some fields to the Author domain class. Without fields, it is not considered as a domain object and therefore it is not mapped to the database. Each field represents a database table column.

  1. package com.app
  2. class Author {
  3. String name
  4. static constraints = {}
  5. }
Service layer
  1. Create a new service in the same way as the domain object. IDEA new Grails Service

  2. Create getAuthors method that returns list of authors from the database.

  1. package com.app
  2. import grails.transaction.Transactional
  3. @Transactional
  4. class AuthorService {
  5. List<Author> getAuthors() {
  6. return Author.list()
  7. }
  8. }
Data for development in application bootstrap

When the application starts, we want to create few authors in the database. So we get some initial data for easy development. Open BootStrap.groovy and save few authors into the database.

The code will be executed only in development environment. That means no database records will be created when running application on test, production or any other environment.

  1. import com.app.Author
  2. import grails.util.Environment
  3. class BootStrap {
  4. def init = {
  5. servletContext -> if (Environment.current ==
  6. Environment.DEVELOPMENT) {
  7. Author raymond = new Author(name: 'Raymond')
  8. raymond.save(failOnError: true)
  9. Author pug = new Author(name: 'Pug')
  10. pug.save(failOnError: true)
  11. }
  12. }
  13. def destroy = { }
  14. }
Using service in Vaadin code

Now we are ready to get bean of AuthorService in Vaadin code and get the list of authors from the database. Open MyUI.groovy and put there the following code.

  1. package app
  2. import com.app.Author
  3. import com.app.AuthorService
  4. import com.vaadin.ui.UI
  5. import com.vaadin.ui.VerticalLayout
  6. import com.vaadin.server.VaadinRequest
  7. import com.vaadin.ui.Label
  8. import com.vaadin.grails.Grails
  9. class MyUI extends UI {
  10. @Override
  11. protected void init(VaadinRequest request) {
  12. VerticalLayout layout = new VerticalLayout()
  13. layout.setMargin(true)
  14. AuthorService authorService = Grails.get(AuthorService)
  15. List<Author> authors = authorService.getAuthors()
  16. for (Author author : authors) {
  17. Label label = new Label(author.name)
  18. layout.addComponent(label)
  19. }
  20. setContent(layout)
  21. }
  22. }

Run the application and see the results fetched from the database. We have created domain object Author which is mapped to automatically created database table. Then we have created AuthorService that we have got from Spring application context. We have loaded authors from the database and displayed them in Vaadin application as labels. Data from service

Database connection

As we have covered big part of normal work when developing an application, created the persistence layer, maybe next questions are coming. Where are the data stored? What database is used and how to change it?

Grails is using H2 in memory database by default and you can change it to any other database that is supported by Hibernate. In order to update database connection setup, open DataSource.groovy. Just add there the proper parameters and add the dependency to, for example, MySql driver in BuildConfig.groovy. IDEA data source

You can continue with Vaadin on Grails - Multiple UIs