List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
From source file:com.sfs.ucm.controller.SpecificationAction.java
/** * find tests associated with this artifact */// w w w. ja v a 2 s. co m private List<SpecificationRuleTest> findSpecificationRuleTests(SpecificationRule specificationRule) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<SpecificationRuleTest> c = cb.createQuery(SpecificationRuleTest.class); Root<SpecificationRuleTest> obj = c.from(SpecificationRuleTest.class); c.select(obj).where(cb.equal(obj.get("specificationRule"), specificationRule)); return em.createQuery(c).getResultList(); }
From source file:org.querybyexample.jpa.GenericRepository.java
/** * Count the number of E instances./* w w w.j a v a2 s . com*/ * * @param entity a sample entity whose non-null properties may be used as * search hint * @param searchParameters carries additional search information * @return the number of entities matching the search. */ @Transactional(readOnly = true) public int findCount(E entity, SearchParameters sp) { checkNotNull(entity, "The entity cannot be null"); checkNotNull(sp, "The searchParameters cannot be null"); if (sp.hasNamedQuery()) { return byNamedQueryUtil.numberByNamedQuery(sp).intValue(); } CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = builder.createQuery(Long.class); Root<E> root = criteriaQuery.from(type); if (sp.getDistinct()) { criteriaQuery = criteriaQuery.select(builder.countDistinct(root)); } else { criteriaQuery = criteriaQuery.select(builder.count(root)); } // predicate Predicate predicate = getPredicate(root, builder, entity, sp); if (predicate != null) { criteriaQuery = criteriaQuery.where(predicate); } orderByUtil.forceJoinOrder(root, sp); TypedQuery<Long> typedQuery = entityManager.createQuery(criteriaQuery); applyCacheHints(typedQuery, sp); Long count = typedQuery.getSingleResult(); if (count != null) { return count.intValue(); } else { log.warn("findCount returned null"); return 0; } }
From source file:org.broadleafcommerce.cms.admin.server.handler.StructuredContentItemCriteriaCustomPersistenceHandler.java
@Override public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException { Entity entity = persistencePackage.getEntity(); try {/* www . j av a2s .c om*/ PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective(); Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties( StructuredContentItemCriteria.class.getName(), persistencePerspective); Object primaryKey = helper.getPrimaryKey(entity, adminProperties); StructuredContentItemCriteria adminInstance = (StructuredContentItemCriteria) dynamicEntityDao .retrieve(Class.forName(entity.getType()[0]), primaryKey); if (adminInstance.getStructuredContent().getLockedFlag()) { /* This may be an attempt to delete a target item criteria off an otherwise un-edited, production StructuredContent instance */ CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder(); CriteriaQuery<StructuredContent> query = criteriaBuilder.createQuery(StructuredContent.class); Root<StructuredContentImpl> root = query.from(StructuredContentImpl.class); query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE), criteriaBuilder.equal(root.get("originalItemId"), adminInstance.getStructuredContent().getId()))); query.select(root); TypedQuery<StructuredContent> scQuery = dynamicEntityDao.getStandardEntityManager() .createQuery(query); try { StructuredContent myContent = scQuery.getSingleResult(); for (StructuredContentItemCriteria itemCriteria : myContent.getQualifyingItemCriteria()) { if (itemCriteria.getOrderItemMatchRule().equals(adminInstance.getOrderItemMatchRule()) && itemCriteria.getQuantity().equals(adminInstance.getQuantity())) { myContent.getQualifyingItemCriteria().remove(itemCriteria); return; } } throw new RuntimeException("Unable to find an item criteria to delete"); } catch (Exception e) { throw new IllegalArgumentException("Unable to update a locked record"); } } dynamicEntityDao.remove(adminInstance); } catch (Exception e) { LOG.error("Unable to execute persistence activity", e); throw new ServiceException("Unable to remove entity for " + entity.getType()[0], e); } }
From source file:ch.sdi.plugins.oxwall.job.OxSqlJob.java
/** * Checks if the given hash is present in the ow_base_avatar table * * @param aHash//from w ww . j a v a 2 s . c om * @return */ public boolean isAvatarHashPresent(Long aHash) { if (myDryRun) { myLog.debug("DryRun is active. Not checking for duplicate avatar hash"); return false; } // if myDryRun CriteriaBuilder cb = myEntityManager.getCriteriaBuilder(); CriteriaQuery<OxAvatar> criteria = cb.createQuery(OxAvatar.class); Root<OxAvatar> root = criteria.from(OxAvatar.class); ParameterExpression<Long> avatarParam = cb.parameter(Long.class); avatarParam = cb.parameter(Long.class); criteria.select(root).where(cb.equal(root.get("hash"), avatarParam)); TypedQuery<OxAvatar> query = myEntityManager.createQuery(criteria); query.setParameter(avatarParam, aHash); List<OxAvatar> results = query.getResultList(); if (results.size() > 0) { myLog.debug("given avatar hash is already present: " + aHash); return true; } // if results.size() > 0 return false; }
From source file:org.broadleafcommerce.cms.admin.server.handler.StructuredContentItemCriteriaCustomPersistenceHandler.java
@Override public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException { Entity entity = persistencePackage.getEntity(); removeHtmlEncoding(entity);/* www.j av a2 s . com*/ try { PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective(); Map<String, FieldMetadata> adminProperties = helper.getSimpleMergedProperties( StructuredContentItemCriteria.class.getName(), persistencePerspective); Object primaryKey = helper.getPrimaryKey(entity, adminProperties); StructuredContentItemCriteria adminInstance = (StructuredContentItemCriteria) dynamicEntityDao .retrieve(Class.forName(entity.getType()[0]), primaryKey); if (adminInstance.getStructuredContent().getLockedFlag()) { /* This may be an attempt to delete a target item criteria off an otherwise un-edited, production StructuredContent instance */ CriteriaBuilder criteriaBuilder = dynamicEntityDao.getStandardEntityManager().getCriteriaBuilder(); CriteriaQuery<StructuredContent> query = criteriaBuilder.createQuery(StructuredContent.class); Root<StructuredContentImpl> root = query.from(StructuredContentImpl.class); query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE), criteriaBuilder.equal(root.get("originalItemId"), adminInstance.getStructuredContent().getId()))); query.select(root); TypedQuery<StructuredContent> scQuery = dynamicEntityDao.getStandardEntityManager() .createQuery(query); try { checkCriteria: { StructuredContent myContent = scQuery.getSingleResult(); for (StructuredContentItemCriteria itemCriteria : myContent.getQualifyingItemCriteria()) { if (itemCriteria.getOrderItemMatchRule().equals(adminInstance.getOrderItemMatchRule()) && itemCriteria.getQuantity().equals(adminInstance.getQuantity())) { //manually set the values - otherwise unwanted properties will be set itemCriteria.setOrderItemMatchRule( entity.findProperty("orderItemMatchRule").getValue()); itemCriteria .setQuantity(Integer.parseInt(entity.findProperty("quantity").getValue())); adminInstance = itemCriteria; break checkCriteria; } } throw new RuntimeException("Unable to find an item criteria to update"); } } catch (Exception e) { throw new IllegalArgumentException("Unable to update a locked record"); } } else { adminInstance = (StructuredContentItemCriteria) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false); } adminInstance = (StructuredContentItemCriteria) dynamicEntityDao.merge(adminInstance); Entity adminEntity = helper.getRecord(adminProperties, adminInstance, null, null); return adminEntity; } catch (Exception e) { LOG.error("Unable to execute persistence activity", e); throw new ServiceException("Unable to update entity for " + entity.getType()[0], e); } }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * fetch join:/*from ww w . j ava2s. c o m*/ * SELECT e FROM jpa_query_employee e JOIN FETCH e.address */ // @Transactional public void doFrom3() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); e.fetch("address"); e.fetch("department"); e.fetch("projects", JoinType.INNER); // e.fetch("phones"); // e.fetch("directs"); // e.fetch("manager"); c.select(e); // only show the fetched data TypedQuery<Employee> query = em.createQuery(c); List<Employee> result = query.getResultList(); for (Employee emp : result) { System.out.println(emp.getId() + " | " + emp.getName() + " | " + emp.getAddress() + " | " + emp.getDepartment() + " | " + emp.getProjects()); } }
From source file:com.hiperium.dao.common.generic.GenericDAO.java
/** * /*w w w .j a v a 2 s .co m*/ * @param entity * @param bypassCache * @return */ protected Long count(T entity, boolean bypassCache) { this.logger.debug("count - START"); CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = criteriaBuilder.createQuery(Long.class); Root<T> root = criteriaQuery.from(this.entityClass); criteriaQuery.select(criteriaBuilder.count(root)); // Create JPA Query Query query = this.entityManager.createQuery(criteriaQuery); if (bypassCache) { this.setBypassCahe(query); } // get the query results Long result = (Long) query.getSingleResult(); this.logger.debug("count - END: " + result); return result; }
From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java
/** * Get the list of serviceIds available. * * @param modelVersion//from www. j a v a 2 s . com * the model version. * @return the list of serviceIds available. * @since 3.5.1 */ @Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED) public List<String> getRevisions() { final CriteriaBuilder cb = this.em.getCriteriaBuilder(); final CriteriaQuery<String> q = cb.createQuery(String.class); final Root<EnterpriseType> f = q.from(EnterpriseType.class); q.select(f.<String>get(EnterpriseType_.modelVersion)); q.orderBy(cb.asc(f.<String>get(EnterpriseType_.modelVersion))); final TypedQuery<String> typedQuery = this.em.createQuery(q); final List<String> value = typedQuery.getResultList(); return value; }
From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java
private List<String> getFlowProcessTypeIds(final String modelVersion, final String flowId) { final CriteriaBuilder cb = this.em.getCriteriaBuilder(); final CriteriaQuery<String> q = cb.createQuery(String.class); final Root<FlowProcessType> f = q.from(FlowProcessType.class); q.select(f.<String>get(FlowProcessType_.id)); q.where(cb.equal(f.<String>get(FlowProcessType_.modelVersion), modelVersion), cb.equal(f.<String>get(FlowProcessType_.parentId), flowId)); q.orderBy(cb.asc(f.<Long>get(FlowProcessType_.hjid))); final TypedQuery<String> typedQuery = this.em.createQuery(q); final List<String> value = typedQuery.getResultList(); return value; }
From source file:com.qpark.eip.core.model.analysis.AnalysisDao.java
/** * Get the last model version (lexical compare). * * @return the last model version.//from w w w . jav a 2 s . c om * @since 3.5.1 */ @Transactional(value = EipModelAnalysisPersistenceConfig.TRANSACTION_MANAGER_NAME, propagation = Propagation.REQUIRED) public String getLastModelVersion() { String value = null; final CriteriaBuilder cb = this.em.getCriteriaBuilder(); final CriteriaQuery<String> q = cb.createQuery(String.class); final Root<EnterpriseType> f = q.from(EnterpriseType.class); q.select(f.<String>get(EnterpriseType_.modelVersion)); q.orderBy(cb.desc(f.<String>get(EnterpriseType_.modelVersion))); final TypedQuery<String> typedQuery = this.em.createQuery(q); final List<String> list = typedQuery.getResultList(); if (Objects.nonNull(list) && list.size() > 0) { value = list.get(0); } return value; }