More DB schema refactoring. Ensure that all columns are named in code

This commit is contained in:
mposolda 2014-07-21 16:26:42 +02:00
parent 9ff4947abd
commit b3b46339a4
12 changed files with 76 additions and 36 deletions

View file

@ -3,34 +3,44 @@ package org.keycloak.audit.jpa;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
@Entity
@Table(name="EVENT_ENTITY")
public class EventEntity {
@Id
@Column(length = 36)
@Column(name="ID", length = 36)
private String id;
@Column(name="TIME")
private long time;
@Column(name="EVENT")
private String event;
@Column(name="REALM_ID")
private String realmId;
@Column(name="CLIENT_ID")
private String clientId;
@Column(name="USER_ID")
private String userId;
@Column(name="SESSION_ID")
private String sessionId;
@Column(name="IP_ADDRESS")
private String ipAddress;
@Column(name="ERROR")
private String error;
@Column(length = 2550)
@Column(name="DETAILS_JSON", length = 2550)
private String detailsJson;
public String getId() {

View file

@ -4,6 +4,7 @@ import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@ -33,7 +34,7 @@ public class ApplicationEntity extends ClientEntity {
Collection<RoleEntity> roles = new ArrayList<RoleEntity>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="APPLICATION_DEFAULT_ROLES")
@JoinTable(name="APPLICATION_DEFAULT_ROLES", joinColumns = { @JoinColumn(name="APPLICATION_ID")}, inverseJoinColumns = { @JoinColumn(name="ROLE_ID")})
Collection<RoleEntity> defaultRoles = new ArrayList<RoleEntity>();
public boolean isSurrogateAuthRequired() {

View file

@ -18,7 +18,7 @@ import java.util.Map;
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
@Entity
@Table(name="AUTH_PROVIDER_ENTITY")
@Table(name="AUTH_PROVIDER")
@IdClass(AuthenticationProviderEntity.Key.class)
public class AuthenticationProviderEntity {
@ -36,9 +36,11 @@ public class AuthenticationProviderEntity {
private int priority;
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable(name="AUTH_PROVIDER_CONFIG")
@MapKeyColumn(name="NAME")
@Column(name="VALUE")
@CollectionTable(name="AUTH_PROVIDER_CONFIG", joinColumns = {
@JoinColumn(name="REALM_ID", referencedColumnName = "REALM_ID"),
@JoinColumn(name="AUTH_PROVIDER_NAME", referencedColumnName = "PROVIDER_NAME")})
private Map<String, String> config;
public RealmEntity getRealm() {

View file

@ -44,11 +44,13 @@ public abstract class ClientEntity {
protected RealmEntity realm;
@ElementCollection
@CollectionTable(name = "WEB_ORIGINS")
@Column(name="VALUE")
@CollectionTable(name = "WEB_ORIGINS", joinColumns={ @JoinColumn(name="CLIENT_ID") })
protected Set<String> webOrigins = new HashSet<String>();
@ElementCollection
@CollectionTable(name = "REDIRECT_URIS")
@Column(name="VALUE")
@CollectionTable(name = "REDIRECT_URIS", joinColumns={ @JoinColumn(name="CLIENT_ID") })
protected Set<String> redirectUris = new HashSet<String>();
public RealmEntity getRealm() {

View file

@ -25,7 +25,7 @@ import java.io.Serializable;
@Entity
public class CredentialEntity {
@Id
@Column(length = 36)
@Column(name="ID", length = 36)
protected String id;
@Column(name="TYPE")

View file

@ -108,41 +108,40 @@ public class RealmEntity {
@Column(name="EMAIL_THEME")
protected String emailTheme;
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="USER_REQUIRED_CREDS")
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm")
Collection<RequiredCredentialEntity> requiredCredentials = new ArrayList<RequiredCredentialEntity>();
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="AUTH_PROVIDERS")
@OneToMany(cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm")
List<AuthenticationProviderEntity> authenticationProviders = new ArrayList<AuthenticationProviderEntity>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="REALM_APPLICATION", joinColumns={ @JoinColumn(name="APPLICATION_ID") }, inverseJoinColumns={ @JoinColumn(name="REALM_ID") })
Collection<ApplicationEntity> applications = new ArrayList<ApplicationEntity>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "realm")
Collection<RoleEntity> roles = new ArrayList<RoleEntity>();
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable(name="REALM_SMTP_CONFIG")
@MapKeyColumn(name="NAME")
@Column(name="VALUE")
@CollectionTable(name="REALM_SMTP_CONFIG", joinColumns={ @JoinColumn(name="REALM_ID") })
protected Map<String, String> smtpConfig = new HashMap<String, String>();
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable(name="REALM_SOCIAL_CONFIG")
@MapKeyColumn(name="NAME")
@Column(name="VALUE")
@CollectionTable(name="REALM_SOCIAL_CONFIG", joinColumns={ @JoinColumn(name="REALM_ID") })
protected Map<String, String> socialConfig = new HashMap<String, String>();
@ElementCollection
@MapKeyColumn(name="name")
@Column(name="value")
@CollectionTable(name="REALM_LDAP_CONFIG")
@MapKeyColumn(name="NAME")
@Column(name="VALUE")
@CollectionTable(name="REALM_LDAP_CONFIG", joinColumns={ @JoinColumn(name="REALM_ID") })
protected Map<String, String> ldapServerConfig = new HashMap<String, String>();
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
@JoinTable(name="REALM_DEFAULT_ROLES")
@JoinTable(name="REALM_DEFAULT_ROLES", joinColumns = { @JoinColumn(name="REALM_ID")}, inverseJoinColumns = { @JoinColumn(name="ROLE_ID")})
protected Collection<RoleEntity> defaultRoles = new ArrayList<RoleEntity>();
@Column(name="AUDIT_ENABLED")
@ -151,7 +150,8 @@ public class RealmEntity {
protected long auditExpiration;
@ElementCollection
@CollectionTable(name="REALM_AUDIT_LISTENERS")
@Column(name="VALUE")
@CollectionTable(name="REALM_AUDIT_LISTENERS", joinColumns={ @JoinColumn(name="REALM_ID") })
protected Set<String> auditListeners= new HashSet<String>();
@OneToOne

View file

@ -42,7 +42,7 @@ import java.util.Set;
@NamedQuery(name="deleteUsersByRealm", query="delete from UserEntity u where u.realmId = :realmId")
})
@Entity
@Table(name="USER", uniqueConstraints = {
@Table(name="USER_ENTITY", uniqueConstraints = {
@UniqueConstraint(columnNames = { "REALM_ID", "USERNAME" }),
@UniqueConstraint(columnNames = { "REALM_ID", "EMAIL_CONSTRAINT" })
})

View file

@ -2,6 +2,7 @@ package org.keycloak.models.jpa.entities;
import org.keycloak.models.UserModel;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
@ -10,6 +11,8 @@ import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import java.io.Serializable;
/**
@ -20,15 +23,17 @@ import java.io.Serializable;
@NamedQuery(name="deleteUserRequiredActionsByRealm", query="delete from UserRequiredActionEntity action where action.user IN (select u from UserEntity u where realm=:realm)")
})
@Entity
@Table(name="USER_REQUIRED_ACTION")
@IdClass(UserRequiredActionEntity.Key.class)
public class UserRequiredActionEntity {
@Id
@ManyToOne(fetch= FetchType.LAZY)
@JoinColumn(name="userId")
@JoinColumn(name="USER_ID")
protected UserEntity user;
@Id
@Column(name="ACTION")
protected UserModel.RequiredAction action;
public UserModel.RequiredAction getAction() {

View file

@ -5,6 +5,7 @@ import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
@ -16,7 +17,7 @@ import java.io.Serializable;
* @version $Revision: 1 $
*/
@Entity
@Table(name = "ClientUserSessionAscEntity")
@Table(name = "CLIENT_USERSESSION")
@NamedQueries({
@NamedQuery(name = "removeClientUserSessionByRealm", query = "delete from ClientUserSessionAssociationEntity a where a.session IN (select s from UserSessionEntity s where s.realmId = :realmId)"),
@NamedQuery(name = "removeClientUserSessionByUser", query = "delete from ClientUserSessionAssociationEntity a where a.session IN (select s from UserSessionEntity s where s.realmId = :realmId and s.userId = :userId)"),
@ -28,10 +29,11 @@ public class ClientUserSessionAssociationEntity {
@Id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "SESSION_ID")
protected UserSessionEntity session;
@Id
@Column(length = 36)
@Column(name="CLIENT_ID",length = 36)
protected String clientId;
public UserSessionEntity getSession() {

View file

@ -1,16 +1,15 @@
package org.keycloak.models.sessions.jpa.entities;
import org.hibernate.annotations.GenericGenerator;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.Collection;
@ -18,6 +17,7 @@ import java.util.Collection;
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
@Entity
@Table(name = "USER_SESSION")
@NamedQueries({
@NamedQuery(name = "getUserSessionByUser", query = "select s from UserSessionEntity s where s.realmId = :realmId and s.userId = :userId order by s.started, s.id"),
@NamedQuery(name = "getUserSessionByClient", query = "select s from UserSessionEntity s join s.clients c where s.realmId = :realmId and c.clientId = :clientId order by s.started, s.id"),
@ -29,16 +29,22 @@ import java.util.Collection;
public class UserSessionEntity {
@Id
@Column(length = 36)
@Column(name="ID",length = 36)
protected String id;
@Column(name="USER_ID")
protected String userId;
@Column(name="REALM_ID")
protected String realmId;
@Column(name="IP_ADDRESS")
protected String ipAddress;
@Column(name="STARTED")
protected int started;
@Column(name="LAST_SESSION_REFRESH")
protected int lastSessionRefresh;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="session")

View file

@ -6,6 +6,8 @@ import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import java.io.Serializable;
/**
@ -13,6 +15,7 @@ import java.io.Serializable;
* @version $Revision: 1 $
*/
@Entity
@Table(name="USERNAME_LOGIN_FAILURE")
@NamedQueries({
@NamedQuery(name="getAllFailures", query="select failure from UsernameLoginFailureEntity failure"),
@NamedQuery(name = "removeLoginFailuresByRealm", query = "delete from UsernameLoginFailureEntity f where f.realmId = :realmId"),
@ -22,16 +25,23 @@ import java.io.Serializable;
public class UsernameLoginFailureEntity {
@Id
@Column(length = 200)
@Column(name="USERNAME",length = 200)
protected String username;
@Id
@Column(length = 36)
@Column(name="REALM_ID",length = 36)
protected String realmId;
@Column(name="FAILED_LOGIN_NOT_BEFORE")
protected int failedLoginNotBefore;
@Column(name="NUM_FAILURES")
protected int numFailures;
@Column(name="LAST_FAILURE")
protected long lastFailure;
@Column(name="LAST_IP_FAILURE")
protected String lastIPFailure;
public String getUsername() {

View file

@ -66,7 +66,9 @@
"driverDialect": "${keycloak.connectionsJpa.driverDialect:}",
"user": "${keycloak.connectionsJpa.user:sa}",
"password": "${keycloak.connectionsJpa.password:}",
"databaseSchema": "${keycloak.connectionsJpa.databaseSchema:create-drop}"
"databaseSchema": "${keycloak.connectionsJpa.databaseSchema:create-drop}",
"showSql": "${keycloak.connectionsJpa.showSql:false}",
"formatSql": "${keycloak.connectionsJpa.formatSql:true}"
}
},