Example usage for javax.persistence.criteria CriteriaBuilder createQuery

List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder createQuery.

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

From source file:com.qpark.eip.core.spring.lockedoperation.dao.LockedOperationDaoImpl.java

/**
 * @see com.qpark.eip.core.spring.lockedoperation.dao.LockableOperationDao#unlockOperation(com.qpark.eip.core.spring.lockedoperation.LockableOperation)
 *//*from   w w w . j a  v a 2 s  .co  m*/
@Override
@Transactional(value = EipLockedoperationConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public boolean unlockOperation(final LockableOperation operation) {
    this.logger.debug("+unlock# {} {} {}",
            new Object[] { this.hostName, operation.getUUID(), operation.getName() });
    boolean unlocked = false;
    String logString = getOperationLockString(operation);
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<OperationLockControllType> q = cb.createQuery(OperationLockControllType.class);
    Root<OperationLockControllType> c = q.from(OperationLockControllType.class);
    q.where(cb.equal(c.<String>get("operationName"), logString));
    TypedQuery<OperationLockControllType> typedQuery = this.em.createQuery(q);
    try {
        List<OperationLockControllType> locks = typedQuery.getResultList();
        for (OperationLockControllType lock : locks) {
            this.em.remove(this.em.merge(lock));
        }
        unlocked = true;
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        this.logger.debug("-unlock# {} {} {} {}", new Object[] { this.hostName,
                unlocked ? "unlocked" : "not unlocked", operation.getUUID(), operation.getName() });
    }
    return unlocked;
}

From source file:com.vladmihalcea.HibernateCriteriaTest.java

private List<Product> getProducts_Mercifully() {
    return transactionTemplate.execute(new TransactionCallback<List<Product>>() {
        @Override//from   w  w  w. j a  v  a2s  .  co  m
        public List<Product> doInTransaction(TransactionStatus transactionStatus) {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Product> query = cb.createQuery(Product.class);
            Root<Image> imageRoot = query.from(Image.class);
            Join<Image, Product> productJoin = imageRoot.join(Image_.product);
            query.select(productJoin);
            query.distinct(true);
            List<Predicate> criteria = new ArrayList<Predicate>();
            criteria.add(cb.like(cb.lower(productJoin.get(Product_.name)), "%tv%"));
            criteria.add(cb.gt(imageRoot.get(Image_.index), 0));
            query.where(cb.and(criteria.toArray(new Predicate[criteria.size()])));
            return entityManager.createQuery(query).getResultList();
        }
    });
}

From source file:com.qpark.eip.core.spring.lockedoperation.dao.LockedOperationDaoImpl.java

/**
 * @see com.qpark.eip.core.spring.lockedoperation.dao.LockableOperationDao#isLockedByThisServer(com.qpark.eip.core.spring.lockedoperation.LockableOperation)
 *//* w w w.java  2  s . c o m*/
@Override
@Transactional(value = EipLockedoperationConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public synchronized boolean isLockedByThisServer(final LockableOperation operation) {
    boolean lockedByThisServer = false;
    OperationLockControllType value = null;
    String logString = getOperationLockString(operation);
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<OperationLockControllType> q = cb.createQuery(OperationLockControllType.class);
    Root<OperationLockControllType> c = q.from(OperationLockControllType.class);
    q.where(cb.equal(c.<String>get("operationName"), logString));
    TypedQuery<OperationLockControllType> typedQuery = this.em.createQuery(q);
    try {
        value = typedQuery.getSingleResult();
        if (value != null) {
            if (value.getServerIpAddress().equals(this.hostAddress)) {
                lockedByThisServer = true;
            }
            this.logger.debug(" isLockedByThisServer# {} opeation {} {} locked by: {} {}",
                    new Object[] { lockedByThisServer, operation.getUUID(), operation.getName(),
                            value.getServerName(), value.getLockDate() });
        }
    } catch (NoResultException e) {
        lockedByThisServer = false;
    } catch (NonUniqueResultException e) {
        lockedByThisServer = false;
    }
    return lockedByThisServer;
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute//from  www .j a v a  2  s .c  o m
public long countByFullTexts(final String terms) {
    LOGGER.debug("counting by text {} terms {}", persistentClass, terms);
    if (settings.returnsNullIfTermsAreNull() && Strings.isNullOrEmpty(terms)) {
        return 0;
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Long> q = cb.createQuery(Long.class);

    Root<T> p = q.from(persistentClass);
    final Expression<Long> count = newCountExpression(cb, p);
    q.select(count);
    q.where(newFullTextPredicate(cb, p, terms));

    try {
        return entityManager.createQuery(q).getSingleResult();
    } catch (NoResultException e) {
        return 0;
    }
}

From source file:com.sfs.captor.controller.ProjectPackageAction.java

/**
 * Load project packages//from w  w  w . jav a2s  .c om
 */
private void loadList() {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ProjectPackage> c = cb.createQuery(ProjectPackage.class);
    Root<ProjectPackage> obj = c.from(ProjectPackage.class);
    c.select(obj).where(cb.equal(obj.get("project"), this.project)).orderBy(cb.asc(obj.get("id")));
    this.projectPackages = em.createQuery(c).getResultList();
}

From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java

@Override
@TransactionAttribute/*  w w w  . j a va  2s  . c  o m*/
public List<T> findByAttribute(String attributeName, Object attributeValue, long first, long count,
        String... fg) {
    LOGGER.debug("Searching {} bu attribute {} : {}, fg {}",
            new Object[] { persistentClass, attributeName, attributeValue, fg });

    if (fg != null) {
        entityManager.pushFetchPlan();
        entityManager.getFetchPlan().addFetchGroups(fg);
    }

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<T> q = cb.createQuery(persistentClass);
    Root<T> p = q.from(persistentClass);
    q.where(cb.equal(p.get(attributeName), attributeValue));

    TypedQuery<T> query = entityManager.createQuery(q);

    OrmUtils.firstCount(query, first, count);
    List<T> resultList = query.getResultList();

    if (fg != null) {
        entityManager.popFetchPlan();
    }

    return resultList;

}

From source file:com.qpark.eip.core.spring.lockedoperation.dao.LockedOperationDaoImpl.java

/**
 * @see com.qpark.eip.core.spring.lockedoperation.dao.LockableOperationDao#isLockedOperation(com.qpark.eip.core.spring.lockedoperation.LockableOperation)
 *//*  w w w. ja v  a  2s  .  c o m*/
@Override
@Transactional(value = EipLockedoperationConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public synchronized boolean isLockedOperation(final LockableOperation operation) {
    this.logger.debug("+isLocked# {} {} {}",
            new Object[] { this.hostName, operation.getUUID(), operation.getName() });
    if (this.firstRun) {
        this.unlockOperationOnServerStart();
        this.firstRun = false;
    }
    boolean locked = false;
    String logString = getOperationLockString(operation);
    OperationLockControllType value = null;
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<OperationLockControllType> q = cb.createQuery(OperationLockControllType.class);
    Root<OperationLockControllType> c = q.from(OperationLockControllType.class);
    q.where(cb.equal(c.<String>get("operationName"), logString));
    TypedQuery<OperationLockControllType> typedQuery = this.em.createQuery(q);
    try {
        value = typedQuery.getSingleResult();
        if (value != null) {
            this.logger.debug(" isLocked# opeation {} {} locked by: {} {}", new Object[] { operation.getUUID(),
                    operation.getName(), value.getServerName(), value.getLockDate() });
            locked = true;
        }
    } catch (NoResultException e) {
        locked = false;
    } catch (NonUniqueResultException e) {
        locked = true;
    }
    this.logger.debug("-isLocked# {} {} {} {}", new Object[] { this.hostName, locked ? "locked" : "not locked",
            operation.getUUID(), operation.getName() });
    return locked;
}

From source file:com.vladmihalcea.HibernateCriteriaTest.java

private List<Product> getProducts_Mercilessly() {
    return transactionTemplate.execute(new TransactionCallback<List<Product>>() {
        @Override/*from w  w w .  ja  v  a  2 s . c  o m*/
        public List<Product> doInTransaction(TransactionStatus transactionStatus) {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Product> query = cb.createQuery(Product.class);
            Root<Product> product = query.from(Product.class);
            query.select(product);
            query.distinct(true);

            List<Predicate> criteria = new ArrayList<Predicate>();
            criteria.add(cb.like(cb.lower(product.get(Product_.name)), "%tv%"));

            Subquery<Long> subQuery = query.subquery(Long.class);
            Root<Image> infoRoot = subQuery.from(Image.class);
            Join<Image, Product> productJoin = infoRoot.join(Image_.product);
            subQuery.select(productJoin.<Long>get(Product_.id));

            subQuery.where(cb.gt(infoRoot.get(Image_.index), 0));
            criteria.add(cb.in(product.get(Product_.id)).value(subQuery));
            query.where(cb.and(criteria.toArray(new Predicate[criteria.size()])));
            return entityManager.createQuery(query).getResultList();
        }
    });
}

From source file:com.vladmihalcea.HibernateCriteriaTest.java

private List<ImageProductDTO> getImageProductDTOs() {
    return transactionTemplate.execute(new TransactionCallback<List<ImageProductDTO>>() {
        @Override/*from w  ww  .  jav  a 2  s. co  m*/
        public List<ImageProductDTO> doInTransaction(TransactionStatus transactionStatus) {
            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<ImageProductDTO> query = cb.createQuery(ImageProductDTO.class);
            Root<Image> imageRoot = query.from(Image.class);
            Join<Image, Product> productJoin = imageRoot.join(Image_.product);
            query.distinct(true);
            List<Predicate> criteria = new ArrayList<Predicate>();
            criteria.add(cb.like(cb.lower(productJoin.get(Product_.name)), "%tv%"));
            criteria.add(cb.gt(imageRoot.get(Image_.index), 0));
            query.where(cb.and(criteria.toArray(new Predicate[criteria.size()])));
            query.select(cb.construct(ImageProductDTO.class, imageRoot.get(Image_.name),
                    productJoin.get(Product_.name))).orderBy(cb.asc(imageRoot.get(Image_.name)));
            return entityManager.createQuery(query).getResultList();
        }
    });
}

From source file:com.qpark.eip.core.spring.lockedoperation.dao.LockedOperationDaoImpl.java

/**
 * @see com.qpark.eip.core.spring.lockedoperation.dao.LockableOperationDao#lockOperation(com.qpark.eip.core.spring.lockedoperation.LockableOperation)
 *//*  ww w.j  a v a 2s. com*/
@Override
@Transactional(value = EipLockedoperationConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED)
public synchronized boolean lockOperation(final LockableOperation operation) {
    this.logger.debug("+lock# {} {} {}",
            new Object[] { this.hostName, operation.getUUID(), operation.getName() });
    if (this.firstRun) {
        this.unlockOperationOnServerStart();
        this.firstRun = false;
    }
    boolean locked = false;
    String logString = getOperationLockString(operation);
    OperationLockControllType value = null;
    CriteriaBuilder cb = this.em.getCriteriaBuilder();
    CriteriaQuery<OperationLockControllType> q = cb.createQuery(OperationLockControllType.class);
    Root<OperationLockControllType> c = q.from(OperationLockControllType.class);
    q.where(cb.equal(c.<String>get("operationName"), logString));
    TypedQuery<OperationLockControllType> typedQuery = this.em.createQuery(q);
    try {
        value = typedQuery.getSingleResult();
        this.logger.debug(" lock# opeation {} {} locked by: {} {}", new Object[] { operation.getUUID(),
                operation.getName(), value.getServerName(), value.getLockDate() });
    } catch (NoResultException e) {
        value = null;
    } catch (NonUniqueResultException e) {
        return false;
    }
    if (value == null) {
        value = new OperationLockControllType();
        value.setOperationName(logString);
        value.setServerName(this.hostName);
        value.setServerIpAddress(this.hostAddress);
        value.setLockDate(DateUtil.get(new Date()));
        this.em.persist(value);
        locked = true;
    }
    this.logger.debug("-lock# {} {} {} {}", new Object[] { this.hostName, locked ? "locked" : "not locked",
            operation.getUUID(), operation.getName() });
    return locked;
}