You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In addition to querying, sequence APIs also support the manipulation of entity objects. We need to get a sequence object firstly via `sequenceOf`:
9
+
In addition to querying, sequence APIs also support the manipulation of entity objects. We need to define some extension properties first, creating sequence objects via `sequenceOf`:
department = database.sequenceOf(Departments).find { it.name eq "tech" }
32
+
department = database.departments.find { it.name eq "tech" }
32
33
}
33
34
34
-
sequence.add(employee)
35
+
database.employees.add(employee)
35
36
```
36
37
37
38
In this example, we create an employee object and fill its properties with some initial values. Please note the property `department`, whose value is an entity object just obtained from the database via sequence APIs. When we call the `add` function, the ID of the referenced entity will be saved into `Employees` table. The generated SQL is as follows:
It can be seen that there is a `flushChanges` function in the `Entity` interface. This function updates all the changes of the current entity into the database and returns the affected record number after the update completes. Typical usage is to obtain entity objects via sequence APIs first, then modify their property values according to our requirements, finally call the `flushChanges` function to save the modifications.
70
71
71
72
```kotlin
72
-
val employee =sequence.find { it.id eq 5 } ?:return
73
+
val employee =database.employees.find { it.id eq 5 } ?:return
73
74
employee.job ="engineer"
74
75
employee.salary =100
75
76
employee.flushChanges()
@@ -97,7 +98,7 @@ Using `flushChanges`, we also need to note that:
97
98
`Entity` interface also provides a `delete` function, which deletes the entity object in the database, and returns the affected record number after the deletion completes. Typical usage is to obtain entity objects via sequence APIs first, then call the `delete` function to delete them according to our requirements.
98
99
99
100
```kotlin
100
-
val employee =sequence.find { it.id eq 5 } ?:return
101
+
val employee =database.employees.find { it.id eq 5 } ?:return
101
102
employee.delete()
102
103
```
103
104
@@ -115,7 +116,7 @@ Similar to `flushChanges`, we also need to note that:
115
116
There are also some other functions that can delete entities, they are `removeIf` and `clear`. While `removeIf` deletes records in the table that matches a given condition, and `clear` deletes all records in a table. Here, we use `removeIf` to delete all the employees in department 1:
0 commit comments