From f3bd6722728b029cff87c6b567b2166e2545b613 Mon Sep 17 00:00:00 2001 From: Jiri Kuchynka Date: Thu, 16 Jan 2025 17:23:28 +0100 Subject: [PATCH] fix: workaround hibernate bug when saving code references during single step import (#2852) --- .../service/dataImport/StoredDataImporter.kt | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/backend/data/src/main/kotlin/io/tolgee/service/dataImport/StoredDataImporter.kt b/backend/data/src/main/kotlin/io/tolgee/service/dataImport/StoredDataImporter.kt index 923d709bf3..e1ea81f974 100644 --- a/backend/data/src/main/kotlin/io/tolgee/service/dataImport/StoredDataImporter.kt +++ b/backend/data/src/main/kotlin/io/tolgee/service/dataImport/StoredDataImporter.kt @@ -162,13 +162,29 @@ class StoredDataImporter( } private fun saveKeyMetaData(keyEntitiesToSave: Collection) { - keyEntitiesToSave.flatMap { - it.keyMeta?.comments ?: emptyList() - }.also { keyMetaService.saveAllComments(it) } - keyEntitiesToSave.flatMap { - it.keyMeta?.codeReferences ?: emptyList() - }.also { keyMetaService.saveAllCodeReferences(it) } + // hibernate bug workaround: + // saving key metas will cause them to be recreated by hibernate with empty values + // we have to save references to comments and codeReferences before saving key metas + val comments = + keyEntitiesToSave.flatMap { + it.keyMeta?.comments ?: emptyList() + } + val codeReferences = + keyEntitiesToSave.flatMap { + it.keyMeta?.codeReferences ?: emptyList() + } keyMetaService.saveAll(keyMetasToSave) + keyMetaService.saveAllComments(comments) + keyMetaService.saveAllCodeReferences(codeReferences) + + // set links to comments and code references to point to correct (previous) + // instances instead of the new empty ones + comments.groupBy { it.keyMeta }.forEach { (keyMeta, comments) -> + keyMeta.comments = comments.toMutableList() + } + codeReferences.groupBy { it.keyMeta }.forEach { (keyMeta, codeReferences) -> + keyMeta.codeReferences = codeReferences.toMutableList() + } } private fun saveTranslations() {