Search in sources :

Example 61 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project jbpm by kiegroup.

the class AuditQueryCriteriaUtil method createQueryAndCallApplyMetaCriteriaAndGetResult.

@Override
protected <T> List<T> createQueryAndCallApplyMetaCriteriaAndGetResult(QueryWhere queryWhere, CriteriaQuery<T> criteriaQuery, CriteriaBuilder builder) {
    EntityManager em = getEntityManager();
    Object newTx = joinTransaction(em);
    Query query = em.createQuery(criteriaQuery);
    applyMetaCriteriaToQuery(query, queryWhere);
    // execute query
    List<T> result = query.getResultList();
    closeEntityManager(em, newTx);
    return result;
}
Also used : EntityManager(javax.persistence.EntityManager) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Query(javax.persistence.Query) OLD_VALUE_LIST(org.kie.internal.query.QueryParameterIdentifiers.OLD_VALUE_LIST) NODE_INSTANCE_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.NODE_INSTANCE_ID_LIST) VARIABLE_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.VARIABLE_ID_LIST) CORRELATION_KEY_LIST(org.kie.internal.query.QueryParameterIdentifiers.CORRELATION_KEY_LIST) VALUE_LIST(org.kie.internal.query.QueryParameterIdentifiers.VALUE_LIST) NODE_NAME_LIST(org.kie.internal.query.QueryParameterIdentifiers.NODE_NAME_LIST) END_DATE_LIST(org.kie.internal.query.QueryParameterIdentifiers.END_DATE_LIST) VARIABLE_INSTANCE_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.VARIABLE_INSTANCE_ID_LIST) IDENTITY_LIST(org.kie.internal.query.QueryParameterIdentifiers.IDENTITY_LIST) PROCESS_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.PROCESS_ID_LIST) TYPE_LIST(org.kie.internal.query.QueryParameterIdentifiers.TYPE_LIST) NODE_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.NODE_ID_LIST) DATE_LIST(org.kie.internal.query.QueryParameterIdentifiers.DATE_LIST) LAST_VARIABLE_LIST(org.kie.internal.query.QueryParameterIdentifiers.LAST_VARIABLE_LIST) DURATION_LIST(org.kie.internal.query.QueryParameterIdentifiers.DURATION_LIST) EXTERNAL_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.EXTERNAL_ID_LIST) PROCESS_NAME_LIST(org.kie.internal.query.QueryParameterIdentifiers.PROCESS_NAME_LIST) VAR_VALUE_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.VAR_VALUE_ID_LIST) OUTCOME_LIST(org.kie.internal.query.QueryParameterIdentifiers.OUTCOME_LIST) PROCESS_INSTANCE_PARENT_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.PROCESS_INSTANCE_PARENT_ID_LIST) PROCESS_VERSION_LIST(org.kie.internal.query.QueryParameterIdentifiers.PROCESS_VERSION_LIST) START_DATE_LIST(org.kie.internal.query.QueryParameterIdentifiers.START_DATE_LIST) WORK_ITEM_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.WORK_ITEM_ID_LIST) PROCESS_INSTANCE_STATUS_LIST(org.kie.internal.query.QueryParameterIdentifiers.PROCESS_INSTANCE_STATUS_LIST) PROCESS_INSTANCE_ID_LIST(org.kie.internal.query.QueryParameterIdentifiers.PROCESS_INSTANCE_ID_LIST)

Example 62 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project jbpm by kiegroup.

the class AbstractTaskQueryCriteriaUtil method createQueryAndCallApplyMetaCriteriaAndGetResult.

@Override
protected <T> List<T> createQueryAndCallApplyMetaCriteriaAndGetResult(QueryWhere queryWhere, CriteriaQuery<T> criteriaQuery, CriteriaBuilder builder) {
    EntityManager em = getEntityManager();
    Object newTx = joinTransaction(em);
    Query query = em.createQuery(criteriaQuery);
    applyMetaCriteriaToQuery(query, queryWhere);
    // execute query
    List<T> result = query.getResultList();
    // depending on the context, this is done
    // 1. here
    // 1. *outside* of this class (this method is a no-op)
    closeEntityManager(em, newTx);
    return result;
}
Also used : EntityManager(javax.persistence.EntityManager) Query(javax.persistence.Query) CriteriaQuery(javax.persistence.criteria.CriteriaQuery)

Example 63 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project dhis2-core by dhis2.

the class HibernateGenericStore method getAllByAttributes.

@Override
public List<T> getAllByAttributes(List<Attribute> attributes) {
    CriteriaBuilder builder = getCriteriaBuilder();
    CriteriaQuery<T> query = builder.createQuery(getClazz());
    Root<T> root = query.from(getClazz());
    query.select(root).distinct(true);
    List<Predicate> predicates = attributes.stream().map(attribute -> builder.isNotNull(builder.function(FUNCTION_JSONB_EXTRACT_PATH, String.class, root.get("attributeValues"), builder.literal(attribute.getUid())))).collect(Collectors.toList());
    query.where(builder.or(predicates.toArray(new Predicate[predicates.size()])));
    return getSession().createQuery(query).list();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Criteria(org.hibernate.Criteria) JsonAttributeValueBinaryType(org.hisp.dhis.hibernate.jsonb.type.JsonAttributeValueBinaryType) Session(org.hibernate.Session) Function(java.util.function.Function) TypedQuery(javax.persistence.TypedQuery) ObjectDeletionRequestedEvent(org.hisp.dhis.common.ObjectDeletionRequestedEvent) Attribute(org.hisp.dhis.attribute.Attribute) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) GenericStore(org.hisp.dhis.common.GenericStore) NonUniqueResultException(javax.persistence.NonUniqueResultException) Lists(com.google.common.collect.Lists) Predicate(javax.persistence.criteria.Predicate) NativeQuery(org.hibernate.query.NativeQuery) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Query(org.hibernate.query.Query) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Expression(javax.persistence.criteria.Expression) Root(javax.persistence.criteria.Root) AuditLogUtil(org.hisp.dhis.common.AuditLogUtil) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SessionFactory(org.hibernate.SessionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) QueryHints(org.hibernate.annotations.QueryHints) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Order(javax.persistence.criteria.Order) Predicate(javax.persistence.criteria.Predicate)

Example 64 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project dhis2-core by dhis2.

the class HibernateGenericStore method countAllValuesByAttributes.

@Override
public long countAllValuesByAttributes(List<Attribute> attributes) {
    CriteriaBuilder builder = getCriteriaBuilder();
    CriteriaQuery<Long> query = builder.createQuery(Long.class);
    Root<T> root = query.from(getClazz());
    query.select(builder.countDistinct(root));
    List<Predicate> predicates = attributes.stream().map(attribute -> builder.isNotNull(builder.function(FUNCTION_JSONB_EXTRACT_PATH, String.class, root.get("attributeValues"), builder.literal(attribute.getUid())))).collect(Collectors.toList());
    query.where(builder.or(predicates.toArray(new Predicate[predicates.size()])));
    return getSession().createQuery(query).getSingleResult();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) AttributeValue(org.hisp.dhis.attribute.AttributeValue) Criteria(org.hibernate.Criteria) JsonAttributeValueBinaryType(org.hisp.dhis.hibernate.jsonb.type.JsonAttributeValueBinaryType) Session(org.hibernate.Session) Function(java.util.function.Function) TypedQuery(javax.persistence.TypedQuery) ObjectDeletionRequestedEvent(org.hisp.dhis.common.ObjectDeletionRequestedEvent) Attribute(org.hisp.dhis.attribute.Attribute) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) GenericStore(org.hisp.dhis.common.GenericStore) NonUniqueResultException(javax.persistence.NonUniqueResultException) Lists(com.google.common.collect.Lists) Predicate(javax.persistence.criteria.Predicate) NativeQuery(org.hibernate.query.NativeQuery) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Query(org.hibernate.query.Query) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Expression(javax.persistence.criteria.Expression) Root(javax.persistence.criteria.Root) AuditLogUtil(org.hisp.dhis.common.AuditLogUtil) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) SessionFactory(org.hibernate.SessionFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) IOException(java.io.IOException) QueryHints(org.hibernate.annotations.QueryHints) Collectors(java.util.stream.Collectors) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) DetachedCriteria(org.hibernate.criterion.DetachedCriteria) Order(javax.persistence.criteria.Order) Predicate(javax.persistence.criteria.Predicate)

Example 65 with CriteriaQuery

use of javax.persistence.criteria.CriteriaQuery in project mycore by MyCoRe-Org.

the class MCRIFSCommands method checkDerivatesWithProjectIDInMCRFSNODES.

@MCRCommand(syntax = "check mcrfsnodes of derivates with project id {0}", help = "check the entries of MCRFSNODES with project ID {0} that the derivate exists")
public static void checkDerivatesWithProjectIDInMCRFSNODES(String project_id) {
    LOGGER.info("Start check of MCRFSNODES for derivates with project ID {}", project_id);
    if (project_id == null || project_id.length() == 0) {
        LOGGER.error("Project ID missed for check MCRFSNODES entries of derivates with project ID {0}");
        return;
    }
    MCRXMLMetadataManager mgr = MCRXMLMetadataManager.instance();
    EntityManager em = MCREntityManagerProvider.getCurrentEntityManager();
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<String> query = cb.createQuery(String.class);
    Root<MCRFSNODES> nodes = query.from(MCRFSNODES.class);
    AtomicInteger counter = new AtomicInteger();
    em.createQuery(query.distinct(true).select(nodes.get(MCRFSNODES_.owner)).where(cb.like(nodes.get(MCRFSNODES_.owner), project_id + "\\_%"))).getResultList().stream().peek(ignore -> counter.incrementAndGet()).map(MCRObjectID::getInstance).filter(derID -> {
        try {
            return !mgr.exists(derID);
        } catch (IOException e) {
            LOGGER.error("Error while checking existence of {}", derID, e);
            return true;
        }
    }).forEach(missingDerivate -> LOGGER.error("   !!!! Can't find MCRFSNODES entry {} as existing derivate", missingDerivate));
    LOGGER.info("Check done for {} entries", counter.get());
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Transformer(javax.xml.transform.Transformer) Arrays(java.util.Arrays) Date(java.util.Date) StreamResult(javax.xml.transform.stream.StreamResult) FileTime(java.nio.file.attribute.FileTime) MCRStreamQuery(org.mycore.backend.jpa.MCRStreamQuery) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Locale(java.util.Locale) Map(java.util.Map) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) URI(java.net.URI) Method(java.lang.reflect.Method) CriteriaQuery(javax.persistence.criteria.CriteriaQuery) ParameterExpression(javax.persistence.criteria.ParameterExpression) GregorianCalendar(java.util.GregorianCalendar) TimeZone(java.util.TimeZone) StandardOpenOption(java.nio.file.StandardOpenOption) Collectors(java.util.stream.Collectors) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Objects(java.util.Objects) MCREntityManagerProvider(org.mycore.backend.jpa.MCREntityManagerProvider) MCRUtils(org.mycore.common.MCRUtils) List(java.util.List) Attributes2Impl(org.xml.sax.ext.Attributes2Impl) Stream(java.util.stream.Stream) Logger(org.apache.logging.log4j.Logger) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRFilesystemNode(org.mycore.datamodel.ifs.MCRFilesystemNode) SAXException(org.xml.sax.SAXException) MCRContentStoreFactory(org.mycore.datamodel.ifs.MCRContentStoreFactory) Session(org.hibernate.Session) MCRConfiguration(org.mycore.common.config.MCRConfiguration) TypedQuery(javax.persistence.TypedQuery) MCRException(org.mycore.common.MCRException) MessageFormat(java.text.MessageFormat) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) MCRFSNODES_(org.mycore.backend.hibernate.tables.MCRFSNODES_) Charset(java.nio.charset.Charset) MCRDirectory(org.mycore.datamodel.ifs.MCRDirectory) MCRContentStore(org.mycore.datamodel.ifs.MCRContentStore) TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) MCRCommandGroup(org.mycore.frontend.cli.annotation.MCRCommandGroup) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) Root(javax.persistence.criteria.Root) MCRFile(org.mycore.datamodel.ifs.MCRFile) OutputStream(java.io.OutputStream) Iterator(java.util.Iterator) Files(java.nio.file.Files) BufferedWriter(java.io.BufferedWriter) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) NotDirectoryException(java.nio.file.NotDirectoryException) MCRHIBConnection(org.mycore.backend.hibernate.MCRHIBConnection) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) EntityManager(javax.persistence.EntityManager) OutputKeys(javax.xml.transform.OutputKeys) Field(java.lang.reflect.Field) File(java.io.File) NameFileComparator(org.apache.commons.io.comparator.NameFileComparator) TransformerHandler(javax.xml.transform.sax.TransformerHandler) LogManager(org.apache.logging.log4j.LogManager) EntityManager(javax.persistence.EntityManager) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MCRXMLMetadataManager(org.mycore.datamodel.common.MCRXMLMetadataManager) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) IOException(java.io.IOException) MCRFSNODES(org.mycore.backend.hibernate.tables.MCRFSNODES) MCRCommand(org.mycore.frontend.cli.annotation.MCRCommand)

Aggregations

CriteriaQuery (javax.persistence.criteria.CriteriaQuery)155 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)112 EntityManager (javax.persistence.EntityManager)109 Query (javax.persistence.Query)109 TypedQuery (javax.persistence.TypedQuery)69 Predicate (javax.persistence.criteria.Predicate)51 EntityTransaction (javax.persistence.EntityTransaction)45 Root (javax.persistence.criteria.Root)40 List (java.util.List)22 Path (javax.persistence.criteria.Path)18 Test (org.testng.annotations.Test)16 UserTransaction (javax.transaction.UserTransaction)15 Person (org.datanucleus.samples.annotations.models.company.Person)15 Manager (org.datanucleus.samples.jpa.query.Manager)14 Specification (org.springframework.data.jpa.domain.Specification)14 Item (org.jpwh.model.querying.Item)12 Test (org.junit.Test)11 ArrayList (java.util.ArrayList)10 Join (javax.persistence.criteria.Join)10 Session (org.hibernate.Session)9