Search in sources :

Example 86 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project jgnash by ccavanaugh.

the class JpaCommodityDAO method getExchangeNode.

/*
     * @see jgnash.engine.CommodityDAOInterface#getExchangeNode(java.lang.String)
     */
@Override
public ExchangeRate getExchangeNode(final String rateId) {
    ExchangeRate exchangeRate = null;
    try {
        Future<ExchangeRate> future = executorService.submit(() -> {
            emLock.lock();
            try {
                ExchangeRate exchangeRate1 = null;
                final CriteriaBuilder cb = em.getCriteriaBuilder();
                final CriteriaQuery<ExchangeRate> cq = cb.createQuery(ExchangeRate.class);
                final Root<ExchangeRate> root = cq.from(ExchangeRate.class);
                cq.select(root);
                final TypedQuery<ExchangeRate> q = em.createQuery(cq);
                for (final ExchangeRate rate : q.getResultList()) {
                    if (rate.getRateId().equals(rateId)) {
                        exchangeRate1 = rate;
                        break;
                    }
                }
                return exchangeRate1;
            } finally {
                emLock.unlock();
            }
        });
        exchangeRate = future.get();
    } catch (final InterruptedException | ExecutionException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return exchangeRate;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ExchangeRate(jgnash.engine.ExchangeRate) ExecutionException(java.util.concurrent.ExecutionException)

Example 87 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project jgnash by ccavanaugh.

the class JpaCommodityDAO method getCurrencies.

/*
     * @see jgnash.engine.CommodityDAOInterface#getCurrencies()
     */
@Override
@SuppressWarnings("unchecked")
public List<CurrencyNode> getCurrencies() {
    List<CurrencyNode> currencyNodeList = Collections.emptyList();
    try {
        Future<List<CurrencyNode>> future = executorService.submit(() -> {
            emLock.lock();
            try {
                final CriteriaBuilder cb = em.getCriteriaBuilder();
                final CriteriaQuery<CurrencyNode> cq = cb.createQuery(CurrencyNode.class);
                final Root<CurrencyNode> root = cq.from(CurrencyNode.class);
                cq.select(root);
                final TypedQuery<CurrencyNode> q = em.createQuery(cq);
                return stripMarkedForRemoval(new ArrayList<>(q.getResultList()));
            } finally {
                emLock.unlock();
            }
        });
        currencyNodeList = future.get();
    } catch (final InterruptedException | ExecutionException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return currencyNodeList;
}
Also used : CurrencyNode(jgnash.engine.CurrencyNode) CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 88 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project jgnash by ccavanaugh.

the class JpaConfigDAO method getDefaultConfig.

/*
     * @see jgnash.engine.ConfigDAOInterface#getDefaultConfig()
     */
@Override
public synchronized Config getDefaultConfig() {
    Config defaultConfig = null;
    try {
        Future<Config> future = executorService.submit(() -> {
            emLock.lock();
            try {
                Config newConfig;
                try {
                    final CriteriaBuilder cb = em.getCriteriaBuilder();
                    final CriteriaQuery<Config> cq = cb.createQuery(Config.class);
                    final Root<Config> root = cq.from(Config.class);
                    cq.select(root);
                    final TypedQuery<Config> q = em.createQuery(cq);
                    newConfig = q.getSingleResult();
                } catch (final Exception e) {
                    newConfig = new Config();
                    em.getTransaction().begin();
                    em.persist(newConfig);
                    em.getTransaction().commit();
                    logger.info("Generating new default config");
                }
                return newConfig;
            } finally {
                emLock.unlock();
            }
        });
        defaultConfig = future.get();
    } catch (final InterruptedException | ExecutionException e) {
        logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
    }
    return defaultConfig;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Config(jgnash.engine.Config) ExecutionException(java.util.concurrent.ExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 89 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project jgnash by ccavanaugh.

the class JpaEngineDAO method getStoredObjects.

@Override
public List<StoredObject> getStoredObjects() {
    List<StoredObject> list = Collections.emptyList();
    try {
        final Future<List<StoredObject>> future = executorService.submit(() -> {
            emLock.lock();
            try {
                final CriteriaBuilder cb = em.getCriteriaBuilder();
                final CriteriaQuery<StoredObject> cq = cb.createQuery(StoredObject.class);
                final Root<StoredObject> root = cq.from(StoredObject.class);
                cq.select(root);
                final TypedQuery<StoredObject> q = em.createQuery(cq);
                return new ArrayList<>(q.getResultList());
            } finally {
                emLock.unlock();
            }
        });
        list = future.get();
    } catch (final InterruptedException | ExecutionException e) {
        logSevere(JpaEngineDAO.class, e);
    }
    return list;
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) ArrayList(java.util.ArrayList) StoredObject(jgnash.engine.StoredObject) ArrayList(java.util.ArrayList) List(java.util.List) ExecutionException(java.util.concurrent.ExecutionException)

Example 90 with CriteriaBuilder

use of javax.persistence.criteria.CriteriaBuilder in project che by eclipse.

the class JpaRecipeDao method search.

@Override
@Transactional
public List<RecipeImpl> search(String user, List<String> tags, String type, int skipCount, int maxItems) throws ServerException {
    try {
        final EntityManager manager = managerProvider.get();
        final CriteriaBuilder cb = manager.getCriteriaBuilder();
        final CriteriaQuery<RecipeImpl> query = cb.createQuery(RecipeImpl.class);
        final Root<RecipeImpl> fromRecipe = query.from(RecipeImpl.class);
        final ParameterExpression<String> typeParam = cb.parameter(String.class, "recipeType");
        final Predicate checkType = cb.or(cb.isNull(typeParam), cb.equal(fromRecipe.get("type"), typeParam));
        final TypedQuery<RecipeImpl> typedQuery;
        if (tags != null && !tags.isEmpty()) {
            final Join<RecipeImpl, String> tag = fromRecipe.join("tags");
            query.select(cb.construct(RecipeImpl.class, tag.getParent())).where(cb.and(checkType, tag.in(tags))).groupBy(fromRecipe.get("id")).having(cb.equal(cb.count(tag), tags.size()));
            typedQuery = manager.createQuery(query).setParameter("tags", tags);
        } else {
            typedQuery = manager.createQuery(query.where(checkType));
        }
        return typedQuery.setParameter("recipeType", type).setFirstResult(skipCount).setMaxResults(maxItems).getResultList();
    } catch (RuntimeException ex) {
        throw new ServerException(ex.getLocalizedMessage(), ex);
    }
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) EntityManager(javax.persistence.EntityManager) ServerException(org.eclipse.che.api.core.ServerException) RecipeImpl(org.eclipse.che.api.machine.server.recipe.RecipeImpl) Predicate(javax.persistence.criteria.Predicate) Transactional(com.google.inject.persist.Transactional)

Aggregations

CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)168 Test (org.junit.Test)121 EntityManager (javax.persistence.EntityManager)79 TestForIssue (org.hibernate.testing.TestForIssue)38 AbstractMetamodelSpecificTest (org.hibernate.jpa.test.metamodel.AbstractMetamodelSpecificTest)30 Customer (org.hibernate.jpa.test.metamodel.Customer)21 Predicate (javax.persistence.criteria.Predicate)15 Tuple (javax.persistence.Tuple)13 Person (org.hibernate.userguide.model.Person)12 List (java.util.List)11 CriteriaQuery (javax.persistence.criteria.CriteriaQuery)11 ArrayList (java.util.ArrayList)10 Order (org.hibernate.jpa.test.metamodel.Order)10 ExecutionException (java.util.concurrent.ExecutionException)9 Message (org.apache.aries.tx.control.itests.entity.Message)6 TypedQuery (javax.persistence.TypedQuery)5 Root (javax.persistence.criteria.Root)4 Query (javax.persistence.Query)3 SkipForDialect (org.hibernate.testing.SkipForDialect)3 HashSet (java.util.HashSet)2