Create common parent for Jpa*AttributeEntity

Closes #10071
This commit is contained in:
vramik 2022-02-08 15:54:48 +01:00 committed by Hynek Mlnařík
parent 90fe97133c
commit 844c210d86
10 changed files with 128 additions and 252 deletions

View file

@ -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<E> implements JpaChildEntity<E> {
@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());
}
}

View file

@ -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<JpaClientEntity> {
@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<JpaClientEntity> {
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);
}
}

View file

@ -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<JpaClientAttributeEntity> attributes = new HashSet<>();
/**

View file

@ -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<JpaClientScopeEntity> {
@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<JpaClientScopeEntity> {
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);
}
}

View file

@ -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<JpaClientScopeAttributeEntity> attributes = new HashSet<>();
/**

View file

@ -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<JpaRoleEntity> {
@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<JpaRoleEntity> {
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);
}
}

View file

@ -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<JpaRoleAttributeEntity> attributes = new HashSet<>();
/**

View file

@ -51,14 +51,14 @@ limitations under the License.
<column name="id" type="UUID">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="fk_client_scope" type="UUID">
<constraints foreignKeyName="client_scope_attr_fk_client_scope_fkey" references="client_scope(id)" deleteCascade="true"/>
<column name="fk_root" type="UUID">
<constraints foreignKeyName="client_scope_attr_fk_root_fkey" references="client_scope(id)" deleteCascade="true"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="value" type="text"/>
</createTable>
<createIndex tableName="client_scope_attribute" indexName="client_scope_attr_fk_client_scope">
<column name="fk_client_scope"/>
<createIndex tableName="client_scope_attribute" indexName="client_scope_attr_fk_root">
<column name="fk_root"/>
</createIndex>
<createIndex tableName="client_scope_attribute" indexName="client_scope_attr_name_value">
<column name="name"/>

View file

@ -58,14 +58,14 @@ limitations under the License.
<column name="id" type="UUID">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="fk_client" type="UUID">
<constraints foreignKeyName="client_attr_fk_client_fkey" references="client(id)" deleteCascade="true"/>
<column name="fk_root" type="UUID">
<constraints foreignKeyName="client_attr_fk_root_fkey" references="client(id)" deleteCascade="true"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="value" type="text"/>
</createTable>
<createIndex tableName="client_attribute" indexName="client_attr_fk_client">
<column name="fk_client"/>
<createIndex tableName="client_attribute" indexName="client_attr_fk_root">
<column name="fk_root"/>
</createIndex>
<createIndex tableName="client_attribute" indexName="client_attr_name_value">
<column name="name"/>

View file

@ -60,14 +60,14 @@ limitations under the License.
<column name="id" type="UUID">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="fk_role" type="UUID">
<constraints foreignKeyName="role_attr_fk_role_fkey" references="role(id)" deleteCascade="true"/>
<column name="fk_root" type="UUID">
<constraints foreignKeyName="role_attr_fk_root_fkey" references="role(id)" deleteCascade="true"/>
</column>
<column name="name" type="VARCHAR(255)"/>
<column name="value" type="text"/>
</createTable>
<createIndex tableName="role_attribute" indexName="role_attr_fk_role">
<column name="fk_role"/>
<createIndex tableName="role_attribute" indexName="role_attr_fk_root">
<column name="fk_root"/>
</createIndex>
<createIndex tableName="role_attribute" indexName="role_attr_name_value">
<column name="name"/>