Java Code Examples for javax.persistence.TypedQuery#setFlushMode()

The following examples show how to use javax.persistence.TypedQuery#setFlushMode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JPAPermissionTicketStore.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public List<PermissionTicket> findByOwner(String owner, String resourceServerId) {
    TypedQuery<String> query = entityManager.createNamedQuery("findPolicyIdByType", String.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("serverId", resourceServerId);
    query.setParameter("owner", owner);

    List<String> result = query.getResultList();
    List<PermissionTicket> list = new LinkedList<>();
    PermissionTicketStore ticketStore = provider.getStoreFactory().getPermissionTicketStore();

    for (String id : result) {
        PermissionTicket ticket = ticketStore.findById(id, resourceServerId);
        if (Objects.nonNull(ticket)) {
            list.add(ticket);
        }
    }

    return list;
}
 
Example 2
Source File: JPAPolicyStore.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public List<Policy> findDependentPolicies(String policyId, String resourceServerId) {

    TypedQuery<String> query = entityManager.createNamedQuery("findPolicyIdByDependentPolices", String.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("serverId", resourceServerId);
    query.setParameter("policyId", policyId);

    List<String> result = query.getResultList();
    List<Policy> list = new LinkedList<>();
    for (String id : result) {
        Policy policy = provider.getStoreFactory().getPolicyStore().findById(id, resourceServerId);
        if (Objects.nonNull(policy)) {
            list.add(policy);
        }
    }
    return list;
}
 
Example 3
Source File: JPAPolicyStore.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void findByScopeIds(List<String> scopeIds, String resourceId, String resourceServerId, Consumer<Policy> consumer) {
    // Use separate subquery to handle DB2 and MSSSQL
    TypedQuery<PolicyEntity> query;

    if (resourceId == null) {
        query = entityManager.createNamedQuery("findPolicyIdByNullResourceScope", PolicyEntity.class);
    } else {
        query = entityManager.createNamedQuery("findPolicyIdByResourceScope", PolicyEntity.class);
        query.setParameter("resourceId", resourceId);
    }

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("scopeIds", scopeIds);
    query.setParameter("serverId", resourceServerId);

    StoreFactory storeFactory = provider.getStoreFactory();

    query.getResultList().stream()
            .map(id -> new PolicyAdapter(id, entityManager, storeFactory))
            .forEach(consumer::accept);
}
 
Example 4
Source File: JPAPolicyStore.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public List<Policy> findByScopeIds(List<String> scopeIds, String resourceServerId) {
    if (scopeIds==null || scopeIds.isEmpty()) {
        return Collections.emptyList();
    }

    // Use separate subquery to handle DB2 and MSSSQL
    TypedQuery<PolicyEntity> query = entityManager.createNamedQuery("findPolicyIdByScope", PolicyEntity.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("scopeIds", scopeIds);
    query.setParameter("serverId", resourceServerId);

    List<Policy> list = new LinkedList<>();
    StoreFactory storeFactory = provider.getStoreFactory();

    for (PolicyEntity entity : query.getResultList()) {
        list.add(new PolicyAdapter(entity, entityManager, storeFactory));
    }

    return list;
}
 
Example 5
Source File: JPAResourceStore.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public void findByType(String type, String owner, String resourceServerId, Consumer<Resource> consumer) {
    TypedQuery<ResourceEntity> query;

    if (owner != null) {
        query = entityManager.createNamedQuery("findResourceIdByType", ResourceEntity.class);
    } else {
        query = entityManager.createNamedQuery("findResourceIdByTypeNoOwner", ResourceEntity.class);
    }

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("type", type);

    if (owner != null) {
        query.setParameter("ownerId", owner);
    }

    query.setParameter("serverId", resourceServerId);

    StoreFactory storeFactory = provider.getStoreFactory();

    query.getResultList().stream()
            .map(entity -> new ResourceAdapter(entity, entityManager, storeFactory))
            .forEach(consumer);
}
 
Example 6
Source File: ContentDaoImpl.java    From cosmo with Apache License 2.0 6 votes vote down vote up
@Override
public Set<ContentItem> loadChildren(CollectionItem collection, Date timestamp) {

    Set<ContentItem> children = new HashSet<ContentItem>();
    TypedQuery<ContentItem> query = null;

    // use custom HQL query that will eager fetch all associations
    if (timestamp == null) {
        query = this.em.createNamedQuery("contentItem.by.parent", ContentItem.class).setParameter("parent",
                collection);
    } else {
        query = this.em.createNamedQuery("contentItem.by.parent.timestamp", ContentItem.class)
                .setParameter("parent", collection).setParameter("timestamp", timestamp);
    }
    query.setFlushMode(FlushModeType.COMMIT);
    List<ContentItem> results = query.getResultList();
    for (ContentItem content : results) {
        initializeItem(content);
        children.add(content);
    }
    return children;
}
 
Example 7
Source File: JPAPermissionTicketStore.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public List<Resource> findGrantedOwnerResources(String owner, int first, int max) {
    TypedQuery<String> query = entityManager.createNamedQuery("findGrantedOwnerResources", String.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("owner", owner);

    if (first > -1 && max > -1) {
        query.setFirstResult(first);
        query.setMaxResults(max);
    }

    List<String> result = query.getResultList();
    List<Resource> list = new LinkedList<>();
    ResourceStore resourceStore = provider.getStoreFactory().getResourceStore();

    for (String id : result) {
        Resource resource = resourceStore.findById(id, null);

        if (Objects.nonNull(resource)) {
            list.add(resource);
        }
    }

    return list;
}
 
Example 8
Source File: JPAPermissionTicketStore.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public List<PermissionTicket> findByScope(String scopeId, String resourceServerId) {
    if (scopeId==null) {
        return Collections.emptyList();
    }

    // Use separate subquery to handle DB2 and MSSSQL
    TypedQuery<String> query = entityManager.createNamedQuery("findPermissionIdByScope", String.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("scopeId", scopeId);
    query.setParameter("serverId", resourceServerId);

    List<String> result = query.getResultList();
    List<PermissionTicket> list = new LinkedList<>();
    PermissionTicketStore ticketStore = provider.getStoreFactory().getPermissionTicketStore();

    for (String id : result) {
        PermissionTicket ticket = ticketStore.findById(id, resourceServerId);
        if (Objects.nonNull(ticket)) {
            list.add(ticket);
        }
    }

    return list;
}
 
Example 9
Source File: JPAPermissionTicketStore.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public List<PermissionTicket> findByResource(final String resourceId, String resourceServerId) {
    TypedQuery<String> query = entityManager.createNamedQuery("findPermissionIdByResource", String.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("resourceId", resourceId);
    query.setParameter("serverId", resourceServerId);

    List<String> result = query.getResultList();
    List<PermissionTicket> list = new LinkedList<>();
    PermissionTicketStore ticketStore = provider.getStoreFactory().getPermissionTicketStore();

    for (String id : result) {
        PermissionTicket ticket = ticketStore.findById(id, resourceServerId);
        if (Objects.nonNull(ticket)) {
            list.add(ticket);
        }
    }

    return list;
}
 
Example 10
Source File: JPAResourceStore.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public List<Resource> findByUri(String uri, String resourceServerId) {
    TypedQuery<String> query = entityManager.createNamedQuery("findResourceIdByUri", String.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("uri", uri);
    query.setParameter("serverId", resourceServerId);

    List<String> result = query.getResultList();
    List<Resource> list = new LinkedList<>();
    ResourceStore resourceStore = provider.getStoreFactory().getResourceStore();

    for (String id : result) {
        Resource resource = resourceStore.findById(id, resourceServerId);

        if (resource != null) {
            list.add(resource);
        }
    }

    return list;
}
 
Example 11
Source File: JPAPermissionTicketStore.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public List<Resource> findGrantedResources(String requester, String name, int first, int max) {
    TypedQuery<String> query = name == null ? 
            entityManager.createNamedQuery("findGrantedResources", String.class) :
            entityManager.createNamedQuery("findGrantedResourcesByName", String.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("requester", requester);
    
    if (name != null) {
        query.setParameter("resourceName", "%" + name.toLowerCase() + "%");
    }
    
    if (first > -1 && max > -1) {
        query.setFirstResult(first);
        query.setMaxResults(max);
    }

    List<String> result = query.getResultList();
    List<Resource> list = new LinkedList<>();
    ResourceStore resourceStore = provider.getStoreFactory().getResourceStore();

    for (String id : result) {
        Resource resource = resourceStore.findById(id, null);

        if (Objects.nonNull(resource)) {
            list.add(resource);
        }
    }
    
    return list;
}
 
Example 12
Source File: ItemDaoImpl.java    From cosmo with Apache License 2.0 5 votes vote down vote up
protected void checkForDuplicateUid(Item item) {
    // Verify uid not in use
    if (item.getUid() != null) {
        // Lookup item by uid
        TypedQuery<Long> query = this.em.createNamedQuery("itemid.by.uid", Long.class).setParameter("uid",
                item.getUid());
        query.setFlushMode(FlushModeType.COMMIT);
        List<Long> idList = query.getResultList();
        // If uid is in use throw exception
        if (idList.size() > 0) {
            throw new UidInUseException(item.getUid(), "uid " + item.getUid() + " already in use");
        }
    }
}
 
Example 13
Source File: JPAScopeStore.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public Scope findByName(String name, String resourceServerId) {
    try {
        TypedQuery<String> query = entityManager.createNamedQuery("findScopeIdByName", String.class);

        query.setFlushMode(FlushModeType.COMMIT);
        query.setParameter("serverId", resourceServerId);
        query.setParameter("name", name);

        String id = query.getSingleResult();
        return provider.getStoreFactory().getScopeStore().findById(id, resourceServerId);
    } catch (NoResultException nre) {
        return null;
    }
}
 
Example 14
Source File: JPAScopeStore.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public List<Scope> findByResourceServer(final String serverId) {
    TypedQuery<String> query = entityManager.createNamedQuery("findScopeIdByResourceServer", String.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("serverId", serverId);

    List<String> result = query.getResultList();
    List<Scope> list = new LinkedList<>();
    for (String id : result) {
        list.add(provider.getStoreFactory().getScopeStore().findById(id, serverId));
    }
    return list;
}
 
Example 15
Source File: JPAPolicyStore.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void findByResource(String resourceId, String resourceServerId, Consumer<Policy> consumer) {
    TypedQuery<PolicyEntity> query = entityManager.createNamedQuery("findPolicyIdByResource", PolicyEntity.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("resourceId", resourceId);
    query.setParameter("serverId", resourceServerId);

    StoreFactory storeFactory = provider.getStoreFactory();

    query.getResultList().stream()
            .map(entity -> new PolicyAdapter(entity, entityManager, storeFactory))
            .forEach(consumer::accept);
}
 
Example 16
Source File: JPAPolicyStore.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void findByResourceType(String resourceType, String resourceServerId, Consumer<Policy> consumer) {
    TypedQuery<PolicyEntity> query = entityManager.createNamedQuery("findPolicyIdByResourceType", PolicyEntity.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("type", resourceType);
    query.setParameter("serverId", resourceServerId);

    query.getResultList().stream()
            .map(id -> new PolicyAdapter(id, entityManager, provider.getStoreFactory()))
            .forEach(consumer::accept);
}
 
Example 17
Source File: JPAResourceStore.java    From keycloak with Apache License 2.0 5 votes vote down vote up
private void findByOwnerFilter(String ownerId, String resourceServerId, Consumer<Resource> consumer, int firstResult, int maxResult) {
    boolean pagination = firstResult > -1 && maxResult > -1;
    String queryName = pagination ? "findResourceIdByOwnerOrdered" : "findResourceIdByOwner";

    if (resourceServerId == null) {
        queryName = pagination ? "findAnyResourceIdByOwnerOrdered" : "findAnyResourceIdByOwner";
    }

    TypedQuery<String> query = entityManager.createNamedQuery(queryName, String.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("owner", ownerId);

    if (resourceServerId != null) {
        query.setParameter("serverId", resourceServerId);
    }

    if (pagination) {
        query.setFirstResult(firstResult);
        query.setMaxResults(maxResult);
    }

    ResourceStore resourceStore = provider.getStoreFactory().getResourceStore();
    List<String> result = query.getResultList();

    for (String entity : result) {
        Resource cached = resourceStore.findById(entity, resourceServerId);
        
        if (cached != null) {
            consumer.accept(cached);
        }
    }
}
 
Example 18
Source File: ItemDaoImpl.java    From cosmo with Apache License 2.0 5 votes vote down vote up
protected Item findItemByParentAndName(Long userDbId, Long parentDbId, String name) {
    TypedQuery<Item> query = null;
    if (parentDbId != null) {
        query = this.em.createNamedQuery("item.by.ownerId.parentId.name", Item.class)
                .setParameter("ownerid", userDbId).setParameter("parentid", parentDbId).setParameter("name", name);

    } else {
        query = this.em.createNamedQuery("item.by.ownerId.nullParent.name", Item.class)
                .setParameter("ownerid", userDbId).setParameter("name", name);
    }
    query.setFlushMode(FlushModeType.COMMIT);
    List<Item> itemList = query.getResultList();
    return itemList.size() > 0 ? itemList.get(0) : null;
}
 
Example 19
Source File: JPAResourceStore.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void findByScope(List<String> scopes, String resourceServerId, Consumer<Resource> consumer) {
    TypedQuery<ResourceEntity> query = entityManager.createNamedQuery("findResourceIdByScope", ResourceEntity.class);

    query.setFlushMode(FlushModeType.COMMIT);
    query.setParameter("scopeIds", scopes);
    query.setParameter("serverId", resourceServerId);

    StoreFactory storeFactory = provider.getStoreFactory();

    query.getResultList().stream()
            .map(id -> new ResourceAdapter(id, entityManager, storeFactory))
            .forEach(consumer);
}
 
Example 20
Source File: ItemDaoImpl.java    From cosmo with Apache License 2.0 5 votes vote down vote up
@Override
public Ticket findTicket(String key) {
    if (key == null) {
        throw new IllegalArgumentException("key cannot be null");
    }

    // prevent auto flushing when looking up ticket
    this.em.setFlushMode(FlushModeType.COMMIT);
    TypedQuery<Ticket> query = this.em.createNamedQuery("ticket.by.key", Ticket.class).setParameter("key", key);
    query.setFlushMode(FlushModeType.COMMIT);
    List<Ticket> ticketList = query.getResultList();
    return ticketList.size() > 0 ? ticketList.get(0) : null;

}