Cannot access delegate without a transaction error in custom RealmResource

Hello,

I implemented a custom route using the RealmResourceProvider. When I call the route I get the right result but the response code is 500 and I see the following errors in the logs (full log at end of post) :

keycloak_1 | 14:20:42,837 ERROR [org.keycloak.services.error.KeycloakErrorHandler] (default task-1) Uncaught server error: com.fasterxml.jackson.databind.JsonMappingException: Cannot access delegate without a transaction (through reference chain: java.util.LinkedList[0]->org.keycloak.models.jpa.UserAdapter[“roleMappings”])

keycloak_1 | Caused by: java.lang.IllegalStateException: Cannot access delegate without a transaction

Here is my implementation :

public class HeypUsersResourceProvider implements RealmResourceProvider {
/**
 * the keycloak session holds information about the current authentication and the realm that we are currently
 * in
 */
private KeycloakSession session;

public HeypUsersResourceProvider(KeycloakSession session) {
    this.session = session;
}

@GET
@Path("partners/{partnerName}/users")
@Produces({MediaType.APPLICATION_JSON})
public Response getUsersByPartner(@PathParam("partnerName") String partnerName) {
    final List<UserModel> partnerUsers =  session
            .userStorageManager()
            .searchForUserByUserAttribute("partner", partnerName, session.getContext().getRealm());
    return Response.status(Response.Status.OK).entity(partnerUsers).build();
}

@GET
@Path("stores/{storeId}/users")
@Produces({MediaType.APPLICATION_JSON})
public Response getUsersByStore(@PathParam("storeId") String storeId) {
    final List<UserModel> storeUsers = session
            .userStorageManager()
            .searchForUserByUserAttribute("storeId", storeId, session.getContext().getRealm());
    return Response.status(Response.Status.OK).entity(storeUsers).build();
}

@GET
@Path("storegroups/{storeGroupId}/users")
@Produces({MediaType.APPLICATION_JSON})
public Response getUsersByStoreGroup(@PathParam("storeGroupId") String storeGroupId) {
    final List<UserModel> storeGroupUsers =  session
            .userStorageManager()
            .searchForUserByUserAttribute("storeGroupId", storeGroupId, session.getContext().getRealm());
    return Response.status(Response.Status.OK).entity(storeGroupUsers).build();
}

@Override
public Object getResource() {
    return this;
}

@Override
public void close() {

}

}

Full log here : https://pastebin.com/JevuqtQ8

List<UserRepresentation> representations = new ArrayList<>(partnerUsers.size());
for (UserModel user : partnerUsers) {
    representations.add(ModelToRepresentation.toRepresentation(session, session.getContext().getRealm(), user));
}
return Response.status(Response.Status.OK).entity(representations).build();
1 Like

Thanks, that did it :slight_smile: