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

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