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.

sqlcontainer-editing.asciidoc 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. ---
  2. title: Editing
  3. order: 4
  4. layout: page
  5. ---
  6. [[sqlcontainer.editing]]
  7. = Editing
  8. Editing the items ( [classname]#RowItem#s) of SQLContainer can be done similarly
  9. to editing the items of any Vaadin container. [classname]#ColumnProperties# of a
  10. [classname]#RowItem# will automatically notify SQLContainer to make sure that
  11. changes to the items are recorded and will be applied to the database
  12. immediately or on commit, depending on the state of the auto-commit mode.
  13. [[sqlcontainer.editing.adding]]
  14. == Adding items
  15. Adding items to an [classname]#SQLContainer# object can only be done via the
  16. [methodname]#addItem()# method. This method will create a new [classname]#Item#
  17. based on the connected database table column properties. The new item will
  18. either be buffered by the container or committed to the database through the
  19. query delegate depending on whether the auto commit mode (see the next section)
  20. has been enabled.
  21. When an item is added to the container it is impossible to precisely know what
  22. the primary keys of the row will be, or will the row insertion succeed at all.
  23. This is why the SQLContainer will assign an instance of
  24. [classname]#TemporaryRowId# as a [classname]#RowId# for the new item. We will
  25. later describe how to fetch the actual key after the row insertion has
  26. succeeded.
  27. If auto-commit mode is enabled in the [classname]#SQLContainer#, the
  28. [methodname]#addItem()# method will return the final [classname]#RowId# of the
  29. new item.
  30. [[sqlcontainer.editing.fetching]]
  31. == Fetching generated row keys
  32. Since it is a common need to fetch the generated key of a row right after
  33. insertion, a listener/notifier has been added into the
  34. [classname]#QueryDelegate# interface. Currently only the [classname]#TableQuery#
  35. class implements the [classname]#RowIdChangeNotifier# interface, and thus can
  36. notify interested objects of changed row IDs. The events fill be fired after
  37. [methodname]#commit()# in [classname]#TableQuery# has finished; this method is
  38. called by [classname]#SQLContainer# when necessary.
  39. To receive updates on the row IDs, you might use the following code (assuming
  40. container is an instance of [classname]#SQLContainer#). Note that these events
  41. are not fired if auto commit mode is enabled.
  42. ----
  43. app.getDbHelp().getCityContainer().addListener(
  44. new QueryDelegate.RowIdChangeListener() {
  45. public void rowIdChange(RowIdChangeEvent event) {
  46. System.err.println("Old ID: " + event.getOldRowId());
  47. System.err.println("New ID: " + event.getNewRowId());
  48. }
  49. });
  50. ----
  51. [[sqlcontainer.editing.version-column]]
  52. == Version column requirement
  53. If you are using the [classname]#TableQuery# class as the query delegate to the
  54. [classname]#SQLContainer# and need to enable write support, there is an enforced
  55. requirement of specifying a version column name to the [classname]#TableQuery#
  56. instance. The column name can be set to the [classname]#TableQuery# using the
  57. following statement:
  58. ----
  59. tq.setVersionColumn("OPTLOCK");
  60. ----
  61. The version column is preferrably an integer or timestamp typed column in the
  62. table that is attached to the [classname]#TableQuery#. This column will be used
  63. for optimistic locking; before a row modification the [classname]#TableQuery#
  64. will check before that the version column value is the same as it was when the
  65. data was read into the container. This should ensure that no one has modified
  66. the row inbetween the current user's reads and writes.
  67. Note! [classname]#TableQuery# assumes that the database will take care of
  68. updating the version column by either using an actual [literal]#++VERSION++#
  69. column (if supported by the database in question) or by a trigger or a similar
  70. mechanism.
  71. If you are certain that you do not need optimistic locking, but do want to
  72. enable write support, you may point the version column to, for example, a
  73. primary key column of the table.
  74. [[sqlcontainer.editing.autocommit]]
  75. == Auto-commit mode
  76. [classname]#SQLContainer# is by default in transaction mode, which means that
  77. actions that edit, add or remove items are recorded internally by the container.
  78. These actions can be either committed to the database by calling
  79. [methodname]#commit()# or discarded by calling [methodname]#rollback()#.
  80. The container can also be set to auto-commit mode. When this mode is enabled,
  81. all changes will be committed to the database immediately. To enable or disable
  82. the auto-commit mode, call the following method:
  83. ----
  84. public void setAutoCommit(boolean autoCommitEnabled)
  85. ----
  86. It is recommended to leave the auto-commit mode disabled, as it ensures that the
  87. changes can be rolled back if any problems are noticed within the container
  88. items. Using the auto-commit mode will also lead to failure in item addition if
  89. the database table contains non-nullable columns.
  90. [[sqlcontainer.editing.modified-state]]
  91. == Modified state
  92. When used in the transaction mode it may be useful to determine whether the
  93. contents of the [classname]#SQLContainer# have been modified or not. For this
  94. purpose the container provides an [methodname]#isModified()# method, which will
  95. tell the state of the container to the developer. This method will return true
  96. if any items have been added to or removed from the container, as well as if any
  97. value of an existing item has been modified.
  98. Additionally, each [classname]#RowItem# and each [classname]#ColumnProperty#
  99. have [methodname]#isModified()# methods to allow for a more detailed view over
  100. the modification status. Do note that the modification statuses of
  101. [classname]#RowItem# and [classname]#ColumnProperty# objects only depend on
  102. whether or not the actual [classname]#Property# values have been modified. That
  103. is, they do not reflect situations where the whole [classname]#RowItem# has been
  104. marked for removal or has just been added to the container.