Ayfri
05/27/2023, 2:03 AMUser(username = "", password = "").insert()
I don't know if it's a good thing to do, stop me if I'm wrong !
But I can't find any way to get the QueryDSL.insert()
method work, it always says that I'm having the wrong type, even when getting the metamodel by reflection or by a property in my interface, I don't quite get it
Could you help me figuring all this ?Toshihiro Nakamura
05/27/2023, 3:17 AMval db = JdbcDatabase("jdbc:h2:mem:example;DB_CLOSE_DELAY=-1")
interface Common<ENTITY : Any, ID : Any, META : EntityMetamodel<ENTITY, ID, META>> {
val metamodel: META
fun insert(entity: ENTITY): ENTITY {
return db.runQuery(QueryDsl.insert(metamodel).single(entity))
}
}
@KomapperEntity
data class User(@KomapperId val id:Int) {
companion object: Common<User, Int, _User> {
override val metamodel = Meta.user
}
}
@KomapperEntity
data class Game(@KomapperId val id:Int) {
companion object: Common<Game, Int, _Game> {
override val metamodel = Meta.game
}
}
Toshihiro Nakamura
05/27/2023, 3:18 AMval newUser = User.insert(User(id = 1))
val newGame = Game.insert(Game(id = 1))
Ayfri
05/27/2023, 3:24 AMToshihiro Nakamura
05/27/2023, 4:36 AMabstract class Repository<ENTITY : Any, ID : Any, META : EntityMetamodel<ENTITY, ID, META>>(protected val db: JdbcDatabase) {
abstract val metamodel: META
fun insert(entity: ENTITY): ENTITY {
return db.runQuery(QueryDsl.insert(metamodel).single(entity))
}
}
class UserRepository(db: JdbcDatabase) :Repository<User, Int, _User>(db) {
override val metamodel = Meta.user
}
class GameRepository(db: JdbcDatabase) :Repository<Game, Int, _Game>(db) {
override val metamodel = Meta.game
}
Ayfri
05/27/2023, 10:59 AM