Skip to content

Conversation

@keyskey
Copy link
Owner

@keyskey keyskey commented Dec 29, 2021

やったこと

  • UserRepositoryの各メソッドをEntity APIを使って実装することで幾分ラクできないか検証してみた
  • 結果、updateとdeleteはSQL DSLと同じロジックをより少ない行数で再現できた(その分設定を色々追加する手間もかかっている)が、createはダメだった。SQL DSLでいうところのinsertAndGenerateKeyメソッドみたいなものが用意されていないので、create時に生成されるIDを取得する手段がない。

Entity API doesn't provide a way to get generated auto increment key when inserting record. So it's impossible to reproduce the same logic as SQL DSL.

@Repository
class UsersRepositoryImpl(val database: Database): UsersRepository {
// insert時に生成されたidを取得する手段がない
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これが致命的だった。やはりAuto incrementなIDを振る場合Entity APIは使えない。UUIDを使うのであればアリかも。


return user.copy(id = id)
val userDao = buildDao(user)
users().add(userDao)
Copy link
Owner Author

@keyskey keyskey Dec 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addメソッドの返り値はinsert文のaffected rowの数。そんなん要らんのよ 😇

val userDao = buildDao(user)
users().add(userDao)

return user
Copy link
Owner Author

@keyskey keyskey Dec 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ID取ってこれない以上他にユニークなキーがないので検索して引っ張ってくることもできない。とりあえず元のエンティティ返してるけどID = 0が入ってるのでこの返り値は使えない。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants