dave08
08/09/2023, 11:54 AMselect { }
)? It's a pity to have to create an entity object only to map it afterwards to a DTO...? Especially since when using regular selects (w/o templates), we have the Meta classes to help retrieve fields to put them in the DTOs in a type-safe way...Toshihiro Nakamura
08/09/2023, 12:37 PMselectAsRecord
. You can map a Record
to your DTO.
https://www.komapper.org/docs/reference/query/querydsl/select/#selectasrecorddave08
08/09/2023, 12:38 PMin a projection of less than four columns,
dave08
08/09/2023, 12:39 PMdave08
08/09/2023, 12:41 PMQueryDsl.from(a).mapper {
Foo(
a.id,
a.name,
)
}
dave08
08/09/2023, 12:43 PMdave08
08/09/2023, 12:44 PMQueryDsl.from(a).select(a.id, a.name).mapper {
Foo(
a.id,
a.name,
)
}
the mapper has nothing to do with the sql being run, but rather only with mapping the result.Toshihiro Nakamura
08/09/2023, 12:56 PMval list = db.runQuery(QueryDsl.from(a))
list.map {
Foo(
it.id,
it.name,
)
}
val list = db.runQuery(QueryDsl.from(a).select(a.id, a.name))
list.map {
Foo(
it.first,
it.second,
)
}
dave08
08/09/2023, 1:04 PMdave08
08/09/2023, 1:06 PMit[a.id]
would normally return?Toshihiro Nakamura
08/09/2023, 1:11 PMI was wondering if this is just an extra step that Komapper has maybe some internal form that it converts to the Entity, then, I’d have to convert from that to the DTOThe reason Komapper keeps the data as an entity is to make it type safe. I think the cost of instantiating an entity class is cheap enough. I would recommend converting the entities to DTOs.
dave08
08/09/2023, 1:12 PMToshihiro Nakamura
08/09/2023, 1:13 PMdave08
08/09/2023, 1:20 PMoverride fun <T : Any> get(key: ColumnExpression<T, *>): T? {
val value = map[key]
return if (value == null) null else key.exteriorClass.cast(value)
}
Don't you have enough information in the ColumnExpression to know the column's real type (from the entity), and maybe have another function like getAsRealType(...)
instead of exteriorClass which I understand is the type being requested?Toshihiro Nakamura
08/09/2023, 1:30 PMIsn’t it a bit wasteful to load all of them?You may define a separate entity class with only the fields you need.
Toshihiro Nakamura
08/09/2023, 1:30 PMDon’t you have enough information in the ColumnExpression to know the column’s real type (from the entity), and maybe have another function likeI am not sure I understand your idea. It would be great if you could provide an example implementation.instead of exteriorClass which I understand is the type being requested?getAsRealType(...)
dave08
08/09/2023, 1:45 PMget()
backed up by a get(key, asType: KClass)
could do the trick?dave08
08/09/2023, 1:55 PMToshihiro Nakamura
08/10/2023, 10:41 PMso if the column’s definition is right, it should be that the value is too...?Yes