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
As you can see, the `Staff` here is just a simple data class. Ktorm doesn't have any special requirements for this class. It is no longer necessary to define it as an interface, which minimizes the intrusion of the framework to user code. The table object `Staffs` is also defined as a subclass of `BaseTable` and implements the `doCreateEntity` function, in which we get columns' values via square brackets `[]` and fill them into the data object.
@@ -73,8 +75,7 @@ val staffs = database
73
75
Obtain entity objects via sequence APIs, and sorting them by the specific column:
74
76
75
77
```kotlin
76
-
val staffs = database
77
-
.sequenceOf(Staffs)
78
+
val staffs = database.staffs
78
79
.filter { it.sectionId eq 1 }
79
80
.sortedBy { it.id }
80
81
.toList()
@@ -83,8 +84,7 @@ val staffs = database
83
84
Get the number of staffs with a salary of less than 100 thousand in each department:
84
85
85
86
```kotlin
86
-
val counts = database
87
-
.sequenceOf(Staffs)
87
+
val counts = database.staffs
88
88
.filter { it.salary less 100000L }
89
89
.groupingBy { it.sectionId }
90
90
.eachCount()
@@ -97,6 +97,6 @@ For more usages, see the documentation of [SQL DSL](./query.html) and [Entity Se
97
97
However, data classes are not perfect, and that's why Ktorm decided to use `Entity` interfaces when it was originally designed. In fact, even after Ktorm 2.5 released, defining entities as interfaces is still our first choice because there are currently two limitations to using data classes:
98
98
99
99
-**Column bindings are not available:** Since `BaseTable` is directly used as the parent class, we cannot configure the bindings between database columns and entity properties via `bindTo` and `references` while defining our table objects. Therefore, each table object must implement the `doCreateEntity` function, in which we should create our entity objects manually.
100
-
-**Entity manipulation APIs are not available:** Since we define entities as data classes, Ktorm cannot proxy them and cannot detect the status changes of entity objects, which makes it impossible for us to use entity manipulation APIs such as `database.sequenceOf(..).add(..)`, `entity.flushChanges()`, etc. But SQL DSL is not affected. We can still use DSL function such as `database.insert(..) {..}` and `database.update(..) {..}` to perform our data modifications.
100
+
-**Entity manipulation APIs are not available:** Since we define entities as data classes, Ktorm cannot proxy them and cannot detect the status changes of entity objects, which makes it impossible for us to use entity manipulation APIs such as `sequence.add(..)`, `entity.flushChanges()`, etc. But SQL DSL is not affected. We can still use DSL function such as `database.insert(..) {..}` and `database.update(..) {..}` to perform our data modifications.
101
101
102
102
Because of these limitations, you should think carefully before you decide to define your entities as data classes. You might be benefited from using data classes and you would lose other things at the same time. Remember: **Defining entities as interfaces is still our first choice.**
如果 data class 真的那么完美的话,Ktorm 在最初设计的时候就不会决定使用 `Entity` interface 了。事实上,即使在 2.5 版本发布以后,使用 interface 定义实体类仍然是我们的第一选择。与使用 interface 定义实体类相比,使用 data class 目前还存在如下两个限制:
0 commit comments