Results of AttributeConverters are mutable; workaround a regression in H6

Relates to #16551
This commit is contained in:
Alexander Schwartz 2023-01-23 12:18:48 +01:00 committed by Václav Muzikář
parent fa3ba6331e
commit 80f7452950
3 changed files with 25 additions and 1 deletions

View file

@ -1083,9 +1083,9 @@ public class JpaRealmProvider implements RealmProvider, ClientProvider, ClientSc
entity.setRealmId(realm.getId()); entity.setRealmId(realm.getId());
entity.setLocale(locale); entity.setLocale(locale);
entity.setTexts(new HashMap<>()); entity.setTexts(new HashMap<>());
em.persist(entity);
} }
entity.getTexts().put(key, text); entity.getTexts().put(key, text);
em.persist(entity);
} }
@Override @Override

View file

@ -18,8 +18,10 @@
package org.keycloak.models.jpa.converter; package org.keycloak.models.jpa.converter;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import jakarta.persistence.AttributeConverter; import jakarta.persistence.AttributeConverter;
import org.hibernate.type.descriptor.java.MutableMutabilityPlan;
import org.jboss.logging.Logger; import org.jboss.logging.Logger;
import org.keycloak.util.JsonSerialization; import org.keycloak.util.JsonSerialization;
@ -45,4 +47,19 @@ public class MapStringConverter implements AttributeConverter<Map<String, String
return null; return null;
} }
} }
/**
* Mutability plan for this property. 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 <a href="https://hibernate.atlassian.net/browse/HHH-16081">HHH-16081</a>
*/
public static class MapStringConverterMutabilityPlan extends MutableMutabilityPlan<Map<String, String>> {
@Override
protected Map<String, String> deepCopyNotNull(Map<String, String> value) {
return new HashMap<>(value);
}
}
} }

View file

@ -27,6 +27,7 @@ import jakarta.persistence.Id;
import jakarta.persistence.IdClass; import jakarta.persistence.IdClass;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import org.hibernate.annotations.Mutability;
import org.hibernate.annotations.Nationalized; import org.hibernate.annotations.Nationalized;
import org.keycloak.models.jpa.converter.MapStringConverter; import org.keycloak.models.jpa.converter.MapStringConverter;
@ -80,6 +81,12 @@ public class RealmLocalizationTextsEntity {
@Nationalized @Nationalized
@Column(name = "TEXTS") @Column(name = "TEXTS")
@Convert(converter = MapStringConverter.class) @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<String,String> texts; private Map<String,String> texts;
public Map<String,String> getTexts() { public Map<String,String> getTexts() {