fix(account): fix EntityExistsException in syncOfficialBots/createOfficialBotAccount
Build & Test (NowChessSystems) TeamCity build failed
Build & Test (NowChessSystems) TeamCity build failed
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 <noreply@anthropic.com>
This commit is contained in:
@@ -201,26 +201,22 @@ class AccountService:
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
def createOfficialBotAccount(botName: String): Either[AccountError, OfficialBotAccount] =
|
def createOfficialBotAccount(botName: String): Either[AccountError, OfficialBotAccount] =
|
||||||
val id = UUID.randomUUID()
|
|
||||||
val bot = new OfficialBotAccount()
|
val bot = new OfficialBotAccount()
|
||||||
bot.id = id
|
|
||||||
bot.name = botName
|
bot.name = botName
|
||||||
bot.createdAt = Instant.now()
|
bot.createdAt = Instant.now()
|
||||||
bot.token = generateBotToken(id, botName)
|
|
||||||
officialBotAccountRepository.persist(bot)
|
officialBotAccountRepository.persist(bot)
|
||||||
|
bot.token = generateBotToken(bot.id, botName)
|
||||||
Right(bot)
|
Right(bot)
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
def syncOfficialBots(botNames: List[String]): Unit =
|
def syncOfficialBots(botNames: List[String]): Unit =
|
||||||
botNames.foreach { name =>
|
botNames.foreach { name =>
|
||||||
if officialBotAccountRepository.findByName(name).isEmpty then
|
if officialBotAccountRepository.findByName(name).isEmpty then
|
||||||
val id = UUID.randomUUID()
|
|
||||||
val bot = new OfficialBotAccount()
|
val bot = new OfficialBotAccount()
|
||||||
bot.id = id
|
|
||||||
bot.name = name
|
bot.name = name
|
||||||
bot.createdAt = Instant.now()
|
bot.createdAt = Instant.now()
|
||||||
bot.token = generateBotToken(id, name)
|
|
||||||
officialBotAccountRepository.persist(bot)
|
officialBotAccountRepository.persist(bot)
|
||||||
|
bot.token = generateBotToken(bot.id, name)
|
||||||
log.infof("Auto-registered official bot: %s", name)
|
log.infof("Auto-registered official bot: %s", name)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user