From f50177996d3401b7b2a4ef7d51804cb3ee7ae649 Mon Sep 17 00:00:00 2001 From: Bill Burke Date: Mon, 16 Jun 2014 10:15:53 -0400 Subject: [PATCH] better fetch for role mappings --- .../org/keycloak/models/jpa/ClientAdapter.java | 11 ++++++----- .../java/org/keycloak/models/jpa/UserAdapter.java | 14 +++++++++----- .../models/jpa/entities/ScopeMappingEntity.java | 3 ++- .../models/jpa/entities/UserRoleMappingEntity.java | 3 ++- .../src/main/resources/META-INF/persistence.xml | 2 +- .../testsuite/perf/AccessTokenPerfTest.java | 3 ++- 6 files changed, 22 insertions(+), 14 deletions(-) diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java index 08476ef4f2..4360882cc8 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/ClientAdapter.java @@ -198,13 +198,14 @@ public abstract class ClientAdapter implements ClientModel { @Override public Set getScopeMappings() { - TypedQuery query = em.createNamedQuery("clientScopeMappings", ScopeMappingEntity.class); + TypedQuery query = em.createNamedQuery("clientScopeMappingIds", String.class); query.setParameter("client", getEntity()); - List entities = query.getResultList(); + List ids = query.getResultList(); Set roles = new HashSet(); - for (ScopeMappingEntity entity : entities) { - roles.add(new RoleAdapter(realm, em, entity.getRole())); - em.detach(entity); + for (String roleId : ids) { + RoleModel role = realm.getRoleById(roleId); + if (role == null) continue; + roles.add(role); } return roles; } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java b/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java index 586180e1fd..e86b42fa73 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java @@ -2,6 +2,7 @@ package org.keycloak.models.jpa; import org.keycloak.models.ApplicationModel; import org.keycloak.models.AuthenticationLinkModel; +import org.keycloak.models.KeycloakSession; import org.keycloak.models.RealmModel; import org.keycloak.models.RoleContainerModel; import org.keycloak.models.RoleModel; @@ -302,13 +303,16 @@ public class UserAdapter implements UserModel { @Override public Set getRoleMappings() { - TypedQuery query = em.createNamedQuery("userRoleMappings", UserRoleMappingEntity.class); + // we query ids only as the role might be cached and following the @ManyToOne will result in a load + // even if we're getting just the id. + TypedQuery query = em.createNamedQuery("userRoleMappingIds", String.class); query.setParameter("user", getUser()); - List entities = query.getResultList(); + List ids = query.getResultList(); Set roles = new HashSet(); - for (UserRoleMappingEntity entity : entities) { - roles.add(realm.getRoleById(entity.getRole().getId())); - em.detach(entity); + for (String roleId : ids) { + RoleModel roleById = realm.getRoleById(roleId); + if (roleById == null) continue; + roles.add(roleById); } return roles; } diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/ScopeMappingEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/ScopeMappingEntity.java index 8e198987b7..e1a00f9838 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/ScopeMappingEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/ScopeMappingEntity.java @@ -16,7 +16,8 @@ import javax.persistence.NamedQuery; */ @NamedQueries({ @NamedQuery(name="hasScope", query="select m from ScopeMappingEntity m where m.client = :client and m.role = :role"), - @NamedQuery(name="clientScopeMappings", query="select m from ScopeMappingEntity m where m.client = :client") + @NamedQuery(name="clientScopeMappings", query="select m from ScopeMappingEntity m where m.client = :client"), + @NamedQuery(name="clientScopeMappingIds", query="select m.role.id from ScopeMappingEntity m where m.client = :client") }) @Entity public class ScopeMappingEntity { diff --git a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRoleMappingEntity.java b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRoleMappingEntity.java index b5a7a2e41f..647acea93b 100755 --- a/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRoleMappingEntity.java +++ b/model/jpa/src/main/java/org/keycloak/models/jpa/entities/UserRoleMappingEntity.java @@ -10,7 +10,8 @@ import javax.persistence.NamedQuery; */ @NamedQueries({ @NamedQuery(name="userHasRole", query="select m from UserRoleMappingEntity m where m.user = :user and m.role = :role"), - @NamedQuery(name="userRoleMappings", query="select m from UserRoleMappingEntity m where m.user = :user") + @NamedQuery(name="userRoleMappings", query="select m from UserRoleMappingEntity m where m.user = :user"), + @NamedQuery(name="userRoleMappingIds", query="select m.role.id from UserRoleMappingEntity m where m.user = :user") }) @Entity public class UserRoleMappingEntity extends AbstractRoleMappingEntity { diff --git a/testsuite/integration/src/main/resources/META-INF/persistence.xml b/testsuite/integration/src/main/resources/META-INF/persistence.xml index 39dd390bc9..ad9c5138d6 100755 --- a/testsuite/integration/src/main/resources/META-INF/persistence.xml +++ b/testsuite/integration/src/main/resources/META-INF/persistence.xml @@ -29,7 +29,7 @@ - + diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/perf/AccessTokenPerfTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/perf/AccessTokenPerfTest.java index ca22ebddf5..73db611719 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/perf/AccessTokenPerfTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/perf/AccessTokenPerfTest.java @@ -234,7 +234,7 @@ public class AccessTokenPerfTest { @Test public void perfJaxrsClientLogin() { - long ITERATIONS = 100; + long ITERATIONS = 3; JaxrsClientLogin login = new JaxrsClientLogin(); long start = System.currentTimeMillis(); for (int i = 0; i < ITERATIONS; i++) { @@ -252,6 +252,7 @@ public class AccessTokenPerfTest { long start = System.currentTimeMillis(); BrowserLogin login = new BrowserLogin(); for (int i = 0; i < ITERATIONS; i++) { + //System.out.println("----------------------------------"); login.run(); } long end = System.currentTimeMillis() - start;