javax.persistence.FlushModeType Java Examples

The following examples show how to use javax.persistence.FlushModeType. 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> 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 #2
Source File: JPAPolicyStore.java    From keycloak with Apache License 2.0 6 votes vote down vote up
@Override
public List<Policy> findByType(String type, String resourceServerId) {
    TypedQuery<String> query = entityManager.createNamedQuery("findPolicyIdByType", String.class);

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

    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: 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 #4
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 #5
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 #6
Source File: FlushModeTypeHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static FlushModeType getFlushModeType(FlushMode flushMode) {
	if ( flushMode == FlushMode.ALWAYS ) {
		log.debug( "Interpreting Hibernate FlushMode#ALWAYS to JPA FlushModeType#AUTO; may cause problems if relying on FlushMode#ALWAYS-specific behavior" );
		return FlushModeType.AUTO;
	}
	else if ( flushMode == FlushMode.MANUAL ) {
		log.debug( "Interpreting Hibernate FlushMode#MANUAL to JPA FlushModeType#COMMIT; may cause problems if relying on FlushMode#MANUAL-specific behavior" );
		return FlushModeType.COMMIT;
	}
	else if ( flushMode == FlushMode.COMMIT ) {
		return FlushModeType.COMMIT;
	}
	else if ( flushMode == FlushMode.AUTO ) {
		return FlushModeType.AUTO;
	}

	throw new AssertionFailure( "unhandled FlushMode " + flushMode );
}
 
Example #7
Source File: UserDaoImpl.java    From cosmo with Apache License 2.0 6 votes vote down vote up
public User updateUser(User user) {
    // Prevent auto flushing when querying for existing users
    this.em.setFlushMode(FlushModeType.COMMIT);

    User findUser = findUserByUsernameOrEmail(getBaseModelObject(user).getId(), user.getUsername(),
            user.getEmail());

    if (findUser != null) {
        if (findUser.getEmail().equals(user.getEmail())) {
            throw new DuplicateEmailException(user.getEmail());
        } else {
            throw new DuplicateUsernameException(user.getUsername());
        }
    }
    user.updateTimestamp();
    this.em.merge(user);
    this.em.flush();

    return user;
}
 
Example #8
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 #9
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 #10
Source File: FlushModeTypeHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static FlushMode interpretFlushMode(Object value) {
	if ( value == null ) {
		return FlushMode.AUTO;
	}
	if ( FlushMode.class.isInstance( value ) ) {
		return (FlushMode) value;
	}
	else if ( FlushModeType.class.isInstance( value ) ) {
		return getFlushMode( (FlushModeType) value );
	}
	else if ( String.class.isInstance( value ) ) {
		return interpretExternalSetting( (String) value );
	}

	throw new IllegalArgumentException( "Unknown FlushMode source : " + value );

}
 
Example #11
Source File: FlushModeTypeHelper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static FlushMode interpretExternalSetting(String externalName) {
	if ( externalName == null ) {
		return null;
	}

	try {
		log.debug( "Attempting to interpret external setting [" + externalName + "] as FlushMode name" );
		return FlushMode.valueOf( externalName.toUpperCase( Locale.ROOT) );
	}
	catch ( IllegalArgumentException e ) {
		log.debug( "Attempting to interpret external setting [" + externalName + "] as FlushModeType name" );
	}

	try {
		return getFlushMode( FlushModeType.valueOf( externalName.toLowerCase( Locale.ROOT ) ) );
	}
	catch ( IllegalArgumentException ignore ) {
	}

	throw new MappingException( "unknown FlushMode : " + externalName );
}
 
Example #12
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 #13
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 #14
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 #15
Source File: MCRCategoryDAOImpl.java    From mycore with GNU General Public License v3.0 6 votes vote down vote up
private static <T> T withoutFlush(EntityManager entityManager, boolean flushAtEnd,
    Function<EntityManager, T> task) {
    FlushModeType fm = entityManager.getFlushMode();
    entityManager.setFlushMode(FlushModeType.COMMIT);
    try {
        T result = task.apply(entityManager);
        if (flushAtEnd) {
            entityManager.flush();
        }
        return result;
    } catch (RuntimeException e) {
        throw e;
    } finally {
        entityManager.setFlushMode(fm);
    }
}
 
Example #16
Source File: AbstractContainerEntityManagerFactoryIntegrationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testQueryNoPersonsShared() {
	Query q = this.sharedEntityManager.createQuery("select p from Person as p");
	q.setFlushMode(FlushModeType.AUTO);
	List<Person> people = q.getResultList();
	assertEquals(0, people.size());
	try {
		assertNull(q.getSingleResult());
		fail("Should have thrown NoResultException");
	}
	catch (NoResultException ex) {
		// expected
	}
}
 
Example #17
Source File: HibernateJpaOperations.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
private FlushModeType getFlushModeType(AnnotationMetadata annotationMetadata) {
    return annotationMetadata.getAnnotationValuesByType(QueryHint.class)
            .stream()
            .filter(av -> FlushModeType.class.getName().equals(av.stringValue("name").orElse(null)))
            .map(av -> av.enumValue("value", FlushModeType.class))
            .findFirst()
            .orElse(Optional.empty()).orElse(null);
}
 
Example #18
Source File: HibernateJpaOperations.java    From micronaut-data with Apache License 2.0 5 votes vote down vote up
private void flushIfNecessary(
        EntityManager entityManager,
        AnnotationMetadata annotationMetadata) {
    if (annotationMetadata.hasAnnotation(QueryHint.class)) {
        FlushModeType flushModeType = getFlushModeType(annotationMetadata);
        if (flushModeType == FlushModeType.AUTO) {
            entityManager.flush();
        }
    }
}
 
Example #19
Source File: TestBeforeCommitTxListener.java    From cuba with Apache License 2.0 5 votes vote down vote up
private void queryWithFlush(Collection<Entity> managedEntities, EntityManager entityManager) {
    if (!managedEntities.stream().anyMatch(e -> e instanceof User && ((User) e).getLogin().startsWith("TxLstnrTst-")))
        return;

    TypedQuery<User> query = entityManager.createQuery("select u from sec$User u where u.login like ?1", User.class);
    query.setParameter(1, "TxLstnrTst-2-%");
    query.setFlushMode(FlushModeType.AUTO);
    User result = query.getFirstResult();
    assertNotNull(result);
}
 
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;

}
 
Example #21
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 #22
Source File: HibernateSession.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
public FlushModeType getFlushMode() {
    switch (hibernateTemplate.getFlushMode()) {
        case GrailsHibernateTemplate.FLUSH_AUTO:   return FlushModeType.AUTO;
        case GrailsHibernateTemplate.FLUSH_COMMIT: return FlushModeType.COMMIT;
        case GrailsHibernateTemplate.FLUSH_ALWAYS: return FlushModeType.AUTO;
        default:                                   return FlushModeType.AUTO;
    }
}
 
Example #23
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void setFlushMode(final FlushModeType flushMode) {
    final EntityManager entityManager = getEntityManager();
    try {
        final Timer timer = Op.setFlushMode.start(this.timer, this);
        try {
            entityManager.setFlushMode(flushMode);
        } finally {
            timer.stop();
        }
    } finally {
        closeIfNoTx(entityManager);
    }
}
 
Example #24
Source File: HibernateSession.java    From gorm-hibernate5 with Apache License 2.0 5 votes vote down vote up
public void setFlushMode(FlushModeType flushMode) {
    if (flushMode == FlushModeType.AUTO) {
        hibernateTemplate.setFlushMode(GrailsHibernateTemplate.FLUSH_AUTO);
    }
    else if (flushMode == FlushModeType.COMMIT) {
        hibernateTemplate.setFlushMode(GrailsHibernateTemplate.FLUSH_COMMIT);
    }
}
 
Example #25
Source File: AbstractProducedQuery.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public FlushModeType getFlushMode() {
	getProducer().checkOpen();
	return ( flushMode == null ?
			getProducer().getFlushMode() :
			FlushModeTypeHelper.getFlushModeType( flushMode )
	);
}
 
Example #26
Source File: RepositoryMetadataInitializer.java    From deltaspike with Apache License 2.0 5 votes vote down vote up
private FlushModeType extractEntityManagerFlushMode(Class<?> clazz)
{
    EntityManagerConfig config = extractEntityManagerConfig(clazz);
    if (config != null)
    {
        return config.flushMode();
    }
    return null;
}
 
Example #27
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 #28
Source File: JtaEntityManager.java    From tomee with Apache License 2.0 5 votes vote down vote up
public FlushModeType getFlushMode() {
    final EntityManager entityManager = getEntityManager();
    try {
        final Timer timer = Op.getFlushMode.start(this.timer, this);
        try {
            return entityManager.getFlushMode();
        } finally {
            timer.stop();
        }
    } finally {
        closeIfNoTx(entityManager);
    }
}
 
Example #29
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 #30
Source File: JPAResourceStore.java    From keycloak with Apache License 2.0 5 votes vote down vote up
@Override
public void findByTypeInstance(String type, String resourceServerId, Consumer<Resource> consumer) {
    TypedQuery<ResourceEntity> query = entityManager.createNamedQuery("findResourceIdByTypeInstance", ResourceEntity.class);

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

    StoreFactory storeFactory = provider.getStoreFactory();

    query.getResultList().stream()
            .map(entity -> new ResourceAdapter(entity, entityManager, storeFactory))
            .forEach(consumer);
}