---
title: Vaadin On Grails Database Access
order: 36
layout: page
---

[[vaadin-on-grails-database-access]]
= 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
https://twitter.com/VaadinOnGrails[VaadinOnGrails]. This is continuation
of <<VaadinOnGrailsCreateProjectInIntelliJIDEA#, 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]]
Create persistent layer
+++++++++++++++++++++++

1.  Right click on the domain folder and select Create Domain
image:http://vaadinongrails.com/img/idea-new-domain.png[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.
image:http://vaadinongrails.com/img/idea-domain-name.png[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.

....
package com.app

class Author {
  String name
  static constraints = {}
}
....

[[service-layer]]
Service layer
+++++++++++++

1.  Create a new service in the same way as the domain object.
image:http://vaadinongrails.com/img/idea-service-new.png[IDEA new Grails Service]
2.  Create `getAuthors` method that returns list of authors from the
database.

....
package com.app
import grails.transaction.Transactional

@Transactional
class AuthorService {
  List<Author> getAuthors() {
    return Author.list()
  }
}
....

[[data-for-development-in-application-bootstrap]]
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.

....
import com.app.Author
import grails.util.Environment

class BootStrap {

  def init = {
    servletContext -> if (Environment.current ==
        Environment.DEVELOPMENT) {
      Author raymond = new Author(name: 'Raymond')
      raymond.save(failOnError: true)

      Author pug = new Author(name: 'Pug')
      pug.save(failOnError: true)
    }
  }

  def destroy = { }
}
....

[[using-service-in-vaadin-code]]
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.

....
package app

import com.app.Author
import com.app.AuthorService
import com.vaadin.ui.UI
import com.vaadin.ui.VerticalLayout
import com.vaadin.server.VaadinRequest
import com.vaadin.ui.Label
import com.vaadin.grails.Grails

class MyUI extends UI {
  @Override
  protected void init(VaadinRequest request) {
    VerticalLayout layout = new VerticalLayout()
    layout.setMargin(true)
    AuthorService authorService = Grails.get(AuthorService)

    List<Author> authors = authorService.getAuthors()

    for (Author author : authors) {
      Label label = new Label(author.name)
      layout.addComponent(label)
    }

    setContent(layout)
  }
}
....

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.
image:http://vaadinongrails.com/img/data-from-service.png[Data from service]

[[database-connection]]
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`.
image:http://vaadinongrails.com/img/idea-datasource.png[IDEA data source]

You can continue with <<VaadinOnGrailsMultipleUIs#, Vaadin on Grails - Multiple UIs>>
 href='/jquery-ui.git/tree/tests/unit/checkboxradio/common.js?h=1.13.0-rc.3&amp;id=dff0dc0d6af9834f03902fe260b51a15de3ab498'>common.js</a></div><div class='content'>blob: 48c348a3e23ba2c9c6c8c96d8be262059a339a22 (<a href='/jquery-ui.git/plain/tests/unit/checkboxradio/common.js?h=1.13.0-rc.3&amp;id=dff0dc0d6af9834f03902fe260b51a15de3ab498'>plain</a>)
<table summary='blob content' class='blob'>
<tr><td class='linenumbers'><pre><a id='n1' href='#n1'>1</a>
<a id='n2' href='#n2'>2</a>
<a id='n3' href='#n3'>3</a>
<a id='n4' href='#n4'>4</a>
<a id='n5' href='#n5'>5</a>
<a id='n6' href='#n6'>6</a>
<a id='n7' href='#n7'>7</a>
<a id='n8' href='#n8'>8</a>
<a id='n9' href='#n9'>9</a>
<a id='n10' href='#n10'>10</a>
<a id='n11' href='#n11'>11</a>
<a id='n12' href='#n12'>12</a>
<a id='n13' href='#n13'>13</a>
<a id='n14' href='#n14'>14</a>
<a id='n15' href='#n15'>15</a>
<a id='n16' href='#n16'>16</a>
<a id='n17' href='#n17'>17</a>
<a id='n18' href='#n18'>18</a>
<a id='n19' href='#n19'>19</a>
<a id='n20' href='#n20'>20</a>
<a id='n21' href='#n21'>21</a>
<a id='n22' href='#n22'>22</a>
</pre></td>
<td class='lines'><pre><code>