Create proper one-to-many in RealmLocalizationTextsEntity

This avoids Hibernate 6.2.0.CR4 to fail with 'BasicValue cannot be cast to class ToOne'.
It used to work on Hibernate 6.2.0.CR3.
This commit is contained in:
Alexander Schwartz 2023-03-27 10:57:42 +02:00 committed by Václav Muzikář
parent 7a5e265e92
commit 2762e17dc0
4 changed files with 33 additions and 22 deletions

View file

@ -1063,7 +1063,7 @@ public class JpaRealmProvider implements RealmProvider, ClientProvider, ClientSc
private RealmLocalizationTextsEntity getRealmLocalizationTextsEntity(String locale, String realmId) {
RealmLocalizationTextsEntity.RealmLocalizationTextEntityKey key = new RealmLocalizationTextsEntity.RealmLocalizationTextEntityKey();
key.setRealmId(realmId);
key.setRealm(em.getReference(RealmEntity.class, realmId));
key.setLocale(locale);
return em.find(RealmLocalizationTextsEntity.class, key);
}
@ -1086,7 +1086,7 @@ public class JpaRealmProvider implements RealmProvider, ClientProvider, ClientSc
RealmLocalizationTextsEntity entity = getRealmLocalizationTextsEntity(locale, realm.getId());
if(entity == null) {
entity = new RealmLocalizationTextsEntity();
entity.setRealmId(realm.getId());
entity.setRealm(em.getReference(RealmEntity.class, realm.getId()));
entity.setLocale(locale);
entity.setTexts(new HashMap<>());
em.persist(entity);
@ -1099,7 +1099,7 @@ public class JpaRealmProvider implements RealmProvider, ClientProvider, ClientSc
RealmLocalizationTextsEntity entity = new RealmLocalizationTextsEntity();
entity.setTexts(localizationTexts);
entity.setLocale(locale);
entity.setRealmId(realm.getId());
entity.setRealm(em.getReference(RealmEntity.class, realm.getId()));
em.merge(entity);
}

View file

@ -2209,7 +2209,7 @@ public class RealmAdapter implements LegacyRealmModel, JpaModel<RealmEntity> {
}
else {
RealmLocalizationTextsEntity realmLocalizationTextsEntity = new RealmLocalizationTextsEntity();
realmLocalizationTextsEntity.setRealmId(realm.getId());
realmLocalizationTextsEntity.setRealm(realm);
realmLocalizationTextsEntity.setLocale(locale);
realmLocalizationTextsEntity.setTexts(localizationTexts);

View file

@ -237,7 +237,7 @@ public class RealmEntity {
@Column(name="ALLOW_USER_MANAGED_ACCESS")
private boolean allowUserManagedAccess;
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realmId")
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm")
@MapKey(name="locale")
Map<String, RealmLocalizationTextsEntity> realmLocalizationTexts = new HashMap<>();
@ -842,6 +842,13 @@ public class RealmEntity {
return true;
}
public String toString() {
return "Realm{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
@Override
public int hashCode() {
return id.hashCode();

View file

@ -23,8 +23,11 @@ import java.util.Objects;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import org.hibernate.annotations.Mutability;
@ -36,15 +39,15 @@ import org.keycloak.models.jpa.converter.MapStringConverter;
@Table(name = "REALM_LOCALIZATIONS")
public class RealmLocalizationTextsEntity {
static public class RealmLocalizationTextEntityKey implements Serializable {
private String realmId;
private RealmEntity realm;
private String locale;
public String getRealmId() {
return realmId;
public RealmEntity getRealm() {
return realm;
}
public void setRealmId(String realmId) {
this.realmId = realmId;
public void setRealm(RealmEntity realm) {
this.realm = realm;
}
public String getLocale() {
@ -60,19 +63,20 @@ public class RealmLocalizationTextsEntity {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RealmLocalizationTextEntityKey that = (RealmLocalizationTextEntityKey) o;
return Objects.equals(realmId, that.realmId) &&
return Objects.equals(realm, that.realm) &&
Objects.equals(locale, that.locale);
}
@Override
public int hashCode() {
return Objects.hash(realmId, locale);
return Objects.hash(realm, locale);
}
}
@Id
@Column(name = "REALM_ID")
private String realmId;
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name = "REALM_ID")
private RealmEntity realm;
@Id
@Column(name = "LOCALE")
@ -105,20 +109,20 @@ public class RealmLocalizationTextsEntity {
this.locale = locale;
}
public String getRealmId() {
return realmId;
public RealmEntity getRealm() {
return realm;
}
public void setRealmId(String realmId) {
this.realmId = realmId;
public void setRealm(RealmEntity realm) {
this.realm = realm;
}
@Override
public String toString() {
return "LocalizationTextEntity{" +
", text='" + texts + '\'' +
"text='" + texts + '\'' +
", locale='" + locale + '\'' +
", realmId='" + realmId + '\'' +
", realm='" + realm + '\'' +
'}';
}
@ -127,13 +131,13 @@ public class RealmLocalizationTextsEntity {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RealmLocalizationTextsEntity that = (RealmLocalizationTextsEntity) o;
return Objects.equals(realmId, that.realmId) &&
return Objects.equals(realm, that.realm) &&
Objects.equals(locale, that.locale) &&
Objects.equals(texts, that.texts);
}
@Override
public int hashCode() {
return Objects.hash(realmId, locale, texts);
return Objects.hash(realm, locale, texts);
}
}