From 4d54122d24597daa6514d2d526cf31102c682976 Mon Sep 17 00:00:00 2001 From: LQ63 Date: Tue, 9 Jun 2026 14:21:27 +0200 Subject: [PATCH] fix(account): fix EntityExistsException in syncOfficialBots/createOfficialBotAccount MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pre-assigning bot.id on a @GeneratedValue entity causes Hibernate 6 to treat it as detached, throwing PersistentObjectException on persist(). Fix: persist with null id (Hibernate assigns UUID immediately in-memory), then set token on the now-managed entity — dirty tracking commits the token at transaction flush. No second persist() call needed. Co-Authored-By: Claude Sonnet 4.6 --- .../de/nowchess/account/service/AccountService.scala | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/modules/account/src/main/scala/de/nowchess/account/service/AccountService.scala b/modules/account/src/main/scala/de/nowchess/account/service/AccountService.scala index 0f29b36..f1d5f29 100644 --- a/modules/account/src/main/scala/de/nowchess/account/service/AccountService.scala +++ b/modules/account/src/main/scala/de/nowchess/account/service/AccountService.scala @@ -201,26 +201,22 @@ class AccountService: @Transactional def createOfficialBotAccount(botName: String): Either[AccountError, OfficialBotAccount] = - val id = UUID.randomUUID() val bot = new OfficialBotAccount() - bot.id = id bot.name = botName bot.createdAt = Instant.now() - bot.token = generateBotToken(id, botName) officialBotAccountRepository.persist(bot) + bot.token = generateBotToken(bot.id, botName) Right(bot) @Transactional def syncOfficialBots(botNames: List[String]): Unit = botNames.foreach { name => if officialBotAccountRepository.findByName(name).isEmpty then - val id = UUID.randomUUID() val bot = new OfficialBotAccount() - bot.id = id bot.name = name bot.createdAt = Instant.now() - bot.token = generateBotToken(id, name) officialBotAccountRepository.persist(bot) + bot.token = generateBotToken(bot.id, name) log.infof("Auto-registered official bot: %s", name) }