diff --git a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/JpaAttributeEntity.java b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/JpaAttributeEntity.java new file mode 100644 index 0000000000..6787620c01 --- /dev/null +++ b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/JpaAttributeEntity.java @@ -0,0 +1,101 @@ +/* + * Copyright 2022 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.keycloak.models.map.storage.jpa; + +import java.util.Objects; +import java.util.UUID; +import javax.persistence.Column; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.MappedSuperclass; +import org.hibernate.annotations.Nationalized; + +@MappedSuperclass +public abstract class JpaAttributeEntity implements JpaChildEntity { + + @Id + @Column + @GeneratedValue + private UUID id; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name="fk_root") + private E root; + + @Column + private String name; + + @Nationalized + @Column + private String value; + + public JpaAttributeEntity() { + } + + public JpaAttributeEntity(E root, String name, String value) { + this.root = root; + this.name = name; + this.value = value; + } + + public UUID getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public E getParent() { + return root; + } + + public void setParent(E root) { + this.root = root; + } + + @Override + public int hashCode() { + return getClass().hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (!(obj instanceof JpaAttributeEntity)) return false; + JpaAttributeEntity that = (JpaAttributeEntity) obj; + return Objects.equals(getParent(), that.getParent()) && + Objects.equals(getName(), that.getName()) && + Objects.equals(getValue(), that.getValue()); + } +} diff --git a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/client/entity/JpaClientAttributeEntity.java b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/client/entity/JpaClientAttributeEntity.java index e6b59921ab..a04ce8eaaa 100644 --- a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/client/entity/JpaClientAttributeEntity.java +++ b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/client/entity/JpaClientAttributeEntity.java @@ -16,93 +16,18 @@ */ package org.keycloak.models.map.storage.jpa.client.entity; -import java.util.Objects; -import java.util.UUID; -import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; import javax.persistence.Table; -import org.hibernate.annotations.Nationalized; -import org.keycloak.models.map.storage.jpa.JpaChildEntity; +import org.keycloak.models.map.storage.jpa.JpaAttributeEntity; @Entity @Table(name = "client_attribute") -public class JpaClientAttributeEntity implements JpaChildEntity { - - @Id - @Column - @GeneratedValue - private UUID id; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name="fk_client") - private JpaClientEntity client; - - @Column - private String name; - - @Nationalized - @Column - private String value; +public class JpaClientAttributeEntity extends JpaAttributeEntity { public JpaClientAttributeEntity() { } - public JpaClientAttributeEntity(JpaClientEntity client, String name, String value) { - this.client = client; - this.name = name; - this.value = value; - } - - public UUID getId() { - return id; - } - - public JpaClientEntity getClient() { - return client; - } - - public void setClient(JpaClientEntity client) { - this.client = client; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - @Override - public int hashCode() { - return getClass().hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (!(obj instanceof JpaClientAttributeEntity)) return false; - JpaClientAttributeEntity that = (JpaClientAttributeEntity) obj; - return Objects.equals(getClient(), that.getClient()) && - Objects.equals(getName(), that.getName()) && - Objects.equals(getValue(), that.getValue()); - } - - @Override - public JpaClientEntity getParent() { - return getClient(); + public JpaClientAttributeEntity(JpaClientEntity root, String name, String value) { + super(root, name, value); } } diff --git a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/client/entity/JpaClientEntity.java b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/client/entity/JpaClientEntity.java index 71759908a3..2bd0ebbef9 100644 --- a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/client/entity/JpaClientEntity.java +++ b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/client/entity/JpaClientEntity.java @@ -95,7 +95,7 @@ public class JpaClientEntity extends AbstractClientEntity implements JpaRootEnti @Basic(fetch = FetchType.LAZY) private Boolean enabled; - @OneToMany(mappedBy = "client", cascade = CascadeType.PERSIST, orphanRemoval = true) + @OneToMany(mappedBy = "root", cascade = CascadeType.PERSIST, orphanRemoval = true) private final Set attributes = new HashSet<>(); /** diff --git a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/clientscope/entity/JpaClientScopeAttributeEntity.java b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/clientscope/entity/JpaClientScopeAttributeEntity.java index 2c60f9920b..68a60676bd 100644 --- a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/clientscope/entity/JpaClientScopeAttributeEntity.java +++ b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/clientscope/entity/JpaClientScopeAttributeEntity.java @@ -16,93 +16,18 @@ */ package org.keycloak.models.map.storage.jpa.clientscope.entity; -import java.util.Objects; -import java.util.UUID; -import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; import javax.persistence.Table; -import org.hibernate.annotations.Nationalized; -import org.keycloak.models.map.storage.jpa.JpaChildEntity; +import org.keycloak.models.map.storage.jpa.JpaAttributeEntity; @Entity @Table(name = "client_scope_attribute") -public class JpaClientScopeAttributeEntity implements JpaChildEntity { - - @Id - @Column - @GeneratedValue - private UUID id; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name="fk_client_scope") - private JpaClientScopeEntity clientScope; - - @Column - private String name; - - @Nationalized - @Column - private String value; +public class JpaClientScopeAttributeEntity extends JpaAttributeEntity { public JpaClientScopeAttributeEntity() { } - public JpaClientScopeAttributeEntity(JpaClientScopeEntity clientScope, String name, String value) { - this.clientScope = clientScope; - this.name = name; - this.value = value; - } - - public UUID getId() { - return id; - } - - public JpaClientScopeEntity getClientScope() { - return clientScope; - } - - public void setClientScope(JpaClientScopeEntity clientScope) { - this.clientScope = clientScope; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - @Override - public int hashCode() { - return getClass().hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (!(obj instanceof JpaClientScopeAttributeEntity)) return false; - JpaClientScopeAttributeEntity that = (JpaClientScopeAttributeEntity) obj; - return Objects.equals(getClientScope(), that.getClientScope()) && - Objects.equals(getName(), that.getName()) && - Objects.equals(getValue(), that.getValue()); - } - - @Override - public JpaClientScopeEntity getParent() { - return getClientScope(); + public JpaClientScopeAttributeEntity(JpaClientScopeEntity root, String name, String value) { + super(root, name, value); } } diff --git a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/clientscope/entity/JpaClientScopeEntity.java b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/clientscope/entity/JpaClientScopeEntity.java index 2944a652c5..dda77b2241 100644 --- a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/clientscope/entity/JpaClientScopeEntity.java +++ b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/clientscope/entity/JpaClientScopeEntity.java @@ -82,7 +82,7 @@ public class JpaClientScopeEntity extends AbstractClientScopeEntity implements J @Basic(fetch = FetchType.LAZY) private String name; - @OneToMany(mappedBy = "clientScope", cascade = CascadeType.PERSIST, orphanRemoval = true) + @OneToMany(mappedBy = "root", cascade = CascadeType.PERSIST, orphanRemoval = true) private final Set attributes = new HashSet<>(); /** diff --git a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/role/entity/JpaRoleAttributeEntity.java b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/role/entity/JpaRoleAttributeEntity.java index bbc9eb2f35..46d2fd1fd2 100644 --- a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/role/entity/JpaRoleAttributeEntity.java +++ b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/role/entity/JpaRoleAttributeEntity.java @@ -16,93 +16,18 @@ */ package org.keycloak.models.map.storage.jpa.role.entity; -import java.util.Objects; -import java.util.UUID; -import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; import javax.persistence.Table; -import org.hibernate.annotations.Nationalized; -import org.keycloak.models.map.storage.jpa.JpaChildEntity; +import org.keycloak.models.map.storage.jpa.JpaAttributeEntity; @Entity @Table(name = "role_attribute") -public class JpaRoleAttributeEntity implements JpaChildEntity { - - @Id - @Column - @GeneratedValue - private UUID id; - - @ManyToOne(fetch = FetchType.LAZY) - @JoinColumn(name="fk_role") - private JpaRoleEntity role; - - @Column - private String name; - - @Nationalized - @Column - private String value; +public class JpaRoleAttributeEntity extends JpaAttributeEntity { public JpaRoleAttributeEntity() { } - public JpaRoleAttributeEntity(JpaRoleEntity role, String name, String value) { - this.role = role; - this.name = name; - this.value = value; - } - - public UUID getId() { - return id; - } - - public JpaRoleEntity getRole() { - return role; - } - - public void setRole(JpaRoleEntity role) { - this.role = role; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - @Override - public int hashCode() { - return getClass().hashCode(); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (!(obj instanceof JpaRoleAttributeEntity)) return false; - JpaRoleAttributeEntity that = (JpaRoleAttributeEntity) obj; - return Objects.equals(getRole(), that.getRole()) && - Objects.equals(getName(), that.getName()) && - Objects.equals(getValue(), that.getValue()); - } - - @Override - public JpaRoleEntity getParent() { - return role; + public JpaRoleAttributeEntity(JpaRoleEntity root, String name, String value) { + super(root, name, value); } } diff --git a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/role/entity/JpaRoleEntity.java b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/role/entity/JpaRoleEntity.java index 2ef5377afe..95c13f6b90 100644 --- a/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/role/entity/JpaRoleEntity.java +++ b/model/map-jpa/src/main/java/org/keycloak/models/map/storage/jpa/role/entity/JpaRoleEntity.java @@ -88,7 +88,7 @@ public class JpaRoleEntity extends AbstractRoleEntity implements JpaRootEntity { @Basic(fetch = FetchType.LAZY) private String description; - @OneToMany(mappedBy = "role", cascade = CascadeType.PERSIST, orphanRemoval = true) + @OneToMany(mappedBy = "root", cascade = CascadeType.PERSIST, orphanRemoval = true) private final Set attributes = new HashSet<>(); /** diff --git a/model/map-jpa/src/main/resources/META-INF/client-scopes/jpa-client-scopes-changelog-1.xml b/model/map-jpa/src/main/resources/META-INF/client-scopes/jpa-client-scopes-changelog-1.xml index 88eb7522d3..c1355ccdd8 100644 --- a/model/map-jpa/src/main/resources/META-INF/client-scopes/jpa-client-scopes-changelog-1.xml +++ b/model/map-jpa/src/main/resources/META-INF/client-scopes/jpa-client-scopes-changelog-1.xml @@ -51,14 +51,14 @@ limitations under the License. - - + + - - + + diff --git a/model/map-jpa/src/main/resources/META-INF/clients/jpa-clients-changelog-1.xml b/model/map-jpa/src/main/resources/META-INF/clients/jpa-clients-changelog-1.xml index c2ab179cde..93b65de0fa 100644 --- a/model/map-jpa/src/main/resources/META-INF/clients/jpa-clients-changelog-1.xml +++ b/model/map-jpa/src/main/resources/META-INF/clients/jpa-clients-changelog-1.xml @@ -58,14 +58,14 @@ limitations under the License. - - + + - - + + diff --git a/model/map-jpa/src/main/resources/META-INF/roles/jpa-roles-changelog-1.xml b/model/map-jpa/src/main/resources/META-INF/roles/jpa-roles-changelog-1.xml index fe38c2735c..f92597f55f 100644 --- a/model/map-jpa/src/main/resources/META-INF/roles/jpa-roles-changelog-1.xml +++ b/model/map-jpa/src/main/resources/META-INF/roles/jpa-roles-changelog-1.xml @@ -60,14 +60,14 @@ limitations under the License. - - + + - - + +