You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

VaadinOnGrailsDatabaseAccess.asciidoc 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. ---
  2. title: Vaadin On Grails Database Access
  3. order: 36
  4. layout: page
  5. ---
  6. [[vaadin-on-grails-database-access]]
  7. Vaadin on grails - Database access
  8. ----------------------------------
  9. _Versions used in this tutorial: Grails 2.3.x, Vaadin 7.1.x. News and
  10. updates about Vaadin on Grails are available on
  11. https://twitter.com/VaadinOnGrails[VaadinOnGrails]. This is continuation
  12. of link:VaadinOnGrailsCreateProjectInIntelliJIDEA.asciidoc[Vaadin
  13. on Grails - Create project in IntelliJ IDEA]_
  14. We are going to create persistence a domain class that is automatically
  15. mapped into a database through Hibernate. GORM from Grails framework
  16. will do most of the automatic stuff there.
  17. [[create-persistent-layer]]
  18. Create persistent layer
  19. +++++++++++++++++++++++
  20. 1. Right click on the domain folder and select Create Domain
  21. image:http://vaadinongrails.com/img/idea-new-domain.png[IDEA create domain class]
  22. 2. Fill in the full class name. If we put there only `Author` then the
  23. package name will be automatically added based on the name of the
  24. project.
  25. image:http://vaadinongrails.com/img/idea-domain-name.png[IDEA domain name]
  26. 3. Add some fields to the Author domain class. Without fields, it is
  27. not considered as a domain object and therefore it is not mapped to the
  28. database. Each field represents a database table column.
  29. ....
  30. package com.app
  31. class Author {
  32. String name
  33. static constraints = {}
  34. }
  35. ....
  36. [[service-layer]]
  37. Service layer
  38. +++++++++++++
  39. 1. Create a new service in the same way as the domain object.
  40. image:http://vaadinongrails.com/img/idea-service-new.png[IDEA new Grails Service]
  41. 2. Create `getAuthors` method that returns list of authors from the
  42. database.
  43. ....
  44. package com.app
  45. import grails.transaction.Transactional
  46. @Transactional
  47. class AuthorService {
  48. List<Author> getAuthors() {
  49. return Author.list()
  50. }
  51. }
  52. ....
  53. [[data-for-development-in-application-bootstrap]]
  54. Data for development in application bootstrap
  55. +++++++++++++++++++++++++++++++++++++++++++++
  56. When the application starts, we want to create few authors in the
  57. database. So we get some initial data for easy development. Open
  58. `BootStrap.groovy` and save few authors into the database.
  59. The code will be executed only in development environment. That means no
  60. database records will be created when running application on test,
  61. production or any other environment.
  62. ....
  63. import com.app.Author
  64. import grails.util.Environment
  65. class BootStrap {
  66. def init = {
  67. servletContext -> if (Environment.current ==
  68. Environment.DEVELOPMENT) {
  69. Author raymond = new Author(name: 'Raymond')
  70. raymond.save(failOnError: true)
  71. Author pug = new Author(name: 'Pug')
  72. pug.save(failOnError: true)
  73. }
  74. }
  75. def destroy = { }
  76. }
  77. ....
  78. [[using-service-in-vaadin-code]]
  79. Using service in Vaadin code
  80. ++++++++++++++++++++++++++++
  81. Now we are ready to get bean of `AuthorService` in Vaadin code and get
  82. the list of authors from the database. Open `MyUI.groovy` and put there
  83. the following code.
  84. ....
  85. package app
  86. import com.app.Author
  87. import com.app.AuthorService
  88. import com.vaadin.ui.UI
  89. import com.vaadin.ui.VerticalLayout
  90. import com.vaadin.server.VaadinRequest
  91. import com.vaadin.ui.Label
  92. import com.vaadin.grails.Grails
  93. class MyUI extends UI {
  94. @Override
  95. protected void init(VaadinRequest request) {
  96. VerticalLayout layout = new VerticalLayout()
  97. layout.setMargin(true)
  98. AuthorService authorService = Grails.get(AuthorService)
  99. List<Author> authors = authorService.getAuthors()
  100. for (Author author : authors) {
  101. Label label = new Label(author.name)
  102. layout.addComponent(label)
  103. }
  104. setContent(layout)
  105. }
  106. }
  107. ....
  108. Run the application and see the results fetched from the database. We
  109. have created domain object `Author` which is mapped to automatically
  110. created database table. Then we have created `AuthorService` that we
  111. have got from Spring application context. We have loaded authors from
  112. the database and displayed them in Vaadin application as labels.
  113. image:http://vaadinongrails.com/img/data-from-service.png[Data from service]
  114. [[database-connection]]
  115. Database connection
  116. +++++++++++++++++++
  117. As we have covered big part of normal work when developing an
  118. application, created the persistence layer, maybe next questions are
  119. coming. Where are the data stored? What database is used and how to
  120. change it?
  121. Grails is using H2 in memory database by default and you can change it
  122. to any other database that is supported by Hibernate. In order to update
  123. database connection setup, open `DataSource.groovy`. Just add there the
  124. proper parameters and add the dependency to, for example, MySql driver
  125. in `BuildConfig.groovy`.
  126. image:http://vaadinongrails.com/img/idea-datasource.png[IDEA data source]
  127. You can continue with link:VaadinOnGrailsMultipleUIs.asciidoc[Vaadin on Grails - Multiple UIs]