diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProvider.java b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProvider.java index 737316de9f..c9d379fd7f 100644 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProvider.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/JpaRealmProvider.java @@ -1083,9 +1083,9 @@ public class JpaRealmProvider implements RealmProvider, ClientProvider, ClientSc entity.setRealmId(realm.getId()); entity.setLocale(locale); entity.setTexts(new HashMap<>()); + em.persist(entity); } entity.getTexts().put(key, text); - em.persist(entity); } @Override diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/converter/MapStringConverter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/converter/MapStringConverter.java index 7d2ae840ea..2c80c0be86 100644 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/converter/MapStringConverter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/converter/MapStringConverter.java @@ -18,8 +18,10 @@ package org.keycloak.models.jpa.converter; import java.io.IOException; +import java.util.HashMap; import java.util.Map; import jakarta.persistence.AttributeConverter; +import org.hibernate.type.descriptor.java.MutableMutabilityPlan; import org.jboss.logging.Logger; import org.keycloak.util.JsonSerialization; @@ -45,4 +47,19 @@ public class MapStringConverter implements AttributeConverterHHH-16081 + */ + public static class MapStringConverterMutabilityPlan extends MutableMutabilityPlan> { + + @Override + protected Map deepCopyNotNull(Map value) { + return new HashMap<>(value); + } + } + + } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmLocalizationTextsEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmLocalizationTextsEntity.java index f76acd816f..98794092b2 100644 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmLocalizationTextsEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/RealmLocalizationTextsEntity.java @@ -27,6 +27,7 @@ import jakarta.persistence.Id; import jakarta.persistence.IdClass; import jakarta.persistence.Table; +import org.hibernate.annotations.Mutability; import org.hibernate.annotations.Nationalized; import org.keycloak.models.jpa.converter.MapStringConverter; @@ -80,6 +81,12 @@ public class RealmLocalizationTextsEntity { @Nationalized @Column(name = "TEXTS") @Convert(converter = MapStringConverter.class) + /* + * @Mutability annotation needed in Hibernate 6 as it doesn't assume mutability by default + * in contrast to Hibernate 5. + * This is tracked in the upstream project in https://hibernate.atlassian.net/browse/HHH-16081 + */ + @Mutability(MapStringConverter.MapStringConverterMutabilityPlan.class) private Map texts; public Map getTexts() {