List of usage examples for javax.persistence.criteria CriteriaQuery where
CriteriaQuery<T> where(Predicate... restrictions);
From source file:com.june.app.board.repository.jpa.BoardRepositoryImpl.java
@Override public long boardListCnt(Board vo) { CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); int bbsId = vo.getBbsId(); CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class); Root<Board> from = countQuery.from(Board.class); countQuery.select(criteriaBuilder.count(from)); if (bbsId > 0) { countQuery.where(criteriaBuilder.equal(from.get("bbsId"), bbsId)); }/*from w w w. j av a 2 s .com*/ Long count = em.createQuery(countQuery).getSingleResult(); return count; }
From source file:net.groupbuy.dao.impl.CouponCodeDaoImpl.java
public Page<CouponCode> findPage(Member member, Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<CouponCode> criteriaQuery = criteriaBuilder.createQuery(CouponCode.class); Root<CouponCode> root = criteriaQuery.from(CouponCode.class); criteriaQuery.select(root);/*from www. j a v a 2s .co m*/ if (member != null) { criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member)); } return super.findPage(criteriaQuery, pageable); }
From source file:org.openregistry.core.repository.jpa.JpaReferenceRepository.java
public Region getRegionByCodeOrName(final String code) { final CriteriaBuilder criteriaBuilder = this.entityManager.getCriteriaBuilder(); final CriteriaQuery<JpaRegionImpl> c = criteriaBuilder.createQuery(JpaRegionImpl.class); c.distinct(true);/* w w w .j av a 2 s . co m*/ final Root<JpaRegionImpl> region = c.from(JpaRegionImpl.class); c.where(criteriaBuilder.or(criteriaBuilder.equal(region.get(JpaRegionImpl_.code), code), criteriaBuilder.like(region.get(JpaRegionImpl_.name), code))); try { return this.entityManager.createQuery(c).getSingleResult(); } catch (final Exception e) { log.debug(e.getMessage(), e); return null; } }
From source file:org.broadleafcommerce.cms.admin.server.handler.PageItemCriteriaCustomPersistenceHandler.java
@Override public void remove(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException { Entity entity = persistencePackage.getEntity(); try {/* w w w . j ava2 s . c om*/ PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective(); Map<String, FieldMetadata> adminProperties = helper .getSimpleMergedProperties(PageItemCriteria.class.getName(), persistencePerspective); Object primaryKey = helper.getPrimaryKey(entity, adminProperties); PageItemCriteria adminInstance = (PageItemCriteria) dynamicEntityDao .retrieve(Class.forName(entity.getType()[0]), primaryKey); if (adminInstance.getPage().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<Page> query = criteriaBuilder.createQuery(Page.class); Root<PageImpl> root = query.from(PageImpl.class); query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE), criteriaBuilder.equal(root.get("originalPageId"), adminInstance.getPage().getId()))); query.select(root); TypedQuery<Page> scQuery = dynamicEntityDao.getStandardEntityManager().createQuery(query); try { Page myContent = scQuery.getSingleResult(); for (PageItemCriteria 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:ru.portal.services.TableServiceImpl.java
/** * TODO ManyToMany//from w w w. j a v a2 s . c om * @param entityClass * @param id * @return */ @Override public Map<EntityType<?>, Map<String, String>> findByEntityClassId(String entityClass, String id) { try { Class<?> cl = Class.forName(entityClass); EntityType<?> entityType = em.getEntityManagerFactory().getMetamodel().entity(cl); if (entityType != null && entityType.getBindableJavaType().getAnnotation(PortalTable.class) != null) { if (entityType.getBindableJavaType().getName().equals(entityClass)) { Class<?> bindableJavaType = entityType.getBindableJavaType(); //select CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<?> cq = cb.createQuery(bindableJavaType); Root<?> root = cq.from(User.class); cq.where(cb.equal(root.get("id"), Long.parseLong(id))); TypedQuery<?> query = em.createQuery(cq); ParameterExpression<Long> parameter = cb.parameter(Long.class, "id"); //query.setParameter(parameter, Long.parseLong(id)); //query.unwrap(org.hibernate.Query.class).getQueryString(); Object result = query.getSingleResult(); List<String> columns = getTableOrViewMetaData(entityClass); HashMap<String, String> res = new HashMap<>(columns.size()); Class<? extends Object> clazz = result.getClass(); for (String fieldName : columns) { try { Field field = clazz.getDeclaredField(fieldName); field.setAccessible(true); Object fieldValue = field.get(result); res.put(fieldName, fieldValue.toString()); } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) { Logger.getLogger(TableServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } } System.out.println(res); Map<EntityType<?>, Map<String, String>> hm = new HashMap<>(); hm.put(entityType, res); return hm; } } } catch (ClassNotFoundException ex) { Logger.getLogger(TableServiceImpl.class.getName()).log(Level.SEVERE, null, ex); } return null; }
From source file:net.dontdrinkandroot.persistence.dao.ExampleGeneratedIdEntityDaoImpl.java
@Override @Transactional(readOnly = true)/* w w w . jav a 2s . c o m*/ public ExampleGeneratedIdEntity findWithOthersFetchJoin(final Long id) { final CriteriaBuilder builder = this.getCriteriaBuilder(); final CriteriaQuery<ExampleGeneratedIdEntity> criteriaQuery = builder .createQuery(ExampleGeneratedIdEntity.class); final Root<ExampleGeneratedIdEntity> root = criteriaQuery.from(this.entityClass); root.fetch(ExampleGeneratedIdEntity_.otherEntities); criteriaQuery.where(builder.equal(root.get(ExampleGeneratedIdEntity_.id), id)); return this.findSingle(criteriaQuery); }
From source file:de.hopmann.repositories.cran.service.CRANPackageListingService.java
@PackageListingUpdate public boolean isAvailable(String packageName, Version sourceVersion) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<CRANPackageEntity> query = cb.createQuery(CRANPackageEntity.class); Root<CRANPackageEntity> p = query.from(CRANPackageEntity.class); query.where(cb.and( ///* w w w .j a v a2s . c o m*/ cb.equal(p.get(PackageEntity_.name), packageName), cb.equal(p.get(PackageEntity_.version).get(Version_.versionNumber), sourceVersion.getVersionNumber()))); List<CRANPackageEntity> resultList = entityManager.createQuery(query).getResultList(); return !resultList.isEmpty(); }
From source file:org.broadleafcommerce.cms.admin.server.handler.PageItemCriteriaCustomPersistenceHandler.java
@Override public Entity update(PersistencePackage persistencePackage, DynamicEntityDao dynamicEntityDao, RecordHelper helper) throws ServiceException { Entity entity = persistencePackage.getEntity(); removeHtmlEncoding(entity);// w ww. j av a 2 s.com try { PersistencePerspective persistencePerspective = persistencePackage.getPersistencePerspective(); Map<String, FieldMetadata> adminProperties = helper .getSimpleMergedProperties(PageItemCriteria.class.getName(), persistencePerspective); Object primaryKey = helper.getPrimaryKey(entity, adminProperties); PageItemCriteria adminInstance = (PageItemCriteria) dynamicEntityDao .retrieve(Class.forName(entity.getType()[0]), primaryKey); if (adminInstance.getPage().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<Page> query = criteriaBuilder.createQuery(Page.class); Root<PageImpl> root = query.from(PageImpl.class); query.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("archivedFlag"), Boolean.FALSE), criteriaBuilder.equal(root.get("originalPageId"), adminInstance.getPage().getId()))); query.select(root); TypedQuery<Page> scQuery = dynamicEntityDao.getStandardEntityManager().createQuery(query); try { checkCriteria: { Page myContent = scQuery.getSingleResult(); for (PageItemCriteria 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 = (PageItemCriteria) helper.createPopulatedInstance(adminInstance, entity, adminProperties, false); } adminInstance = (PageItemCriteria) 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:org.fao.geonet.repository.HarvesterSettingRepositoryOverridesImpl.java
@Override public List<Integer> findAllChildIds(int parentid) { CriteriaBuilder criteriaBuilder = _entityManager.getCriteriaBuilder(); CriteriaQuery<Integer> query = criteriaBuilder.createQuery(Integer.class); Root<HarvesterSetting> root = query.from(HarvesterSetting.class); query.where(criteriaBuilder.equal(root.get(HarvesterSetting_.parent), parentid)); query.select(root.get(HarvesterSetting_.id)); return _entityManager.createQuery(query).getResultList(); }
From source file:com.vladmihalcea.HibernateFetchStrategyTest.java
@Test public void test() { final Long productId = transactionTemplate.execute(new TransactionCallback<Long>() { @Override/* w w w . ja v a 2s . c om*/ public Long doInTransaction(TransactionStatus transactionStatus) { Company company = new Company(); company.setName("TV Company"); entityManager.persist(company); Product product = new Product("tvCode"); product.setName("TV"); product.setCompany(company); Image frontImage = new Image(); frontImage.setName("front image"); frontImage.setIndex(0); Image sideImage = new Image(); sideImage.setName("side image"); sideImage.setIndex(1); product.addImage(frontImage); product.addImage(sideImage); WarehouseProductInfo warehouseProductInfo = new WarehouseProductInfo(); warehouseProductInfo.setQuantity(101); product.addWarehouse(warehouseProductInfo); Importer importer = new Importer(); importer.setName("Importer"); entityManager.persist(importer); product.setImporter(importer); entityManager.persist(product); return product.getId(); } }); transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus transactionStatus) { LOG.info("Fetch using find"); Product product = entityManager.find(Product.class, productId); assertNotNull(product); return null; } }); transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus transactionStatus) { LOG.info("Fetch using JPQL"); Product product = entityManager .createQuery("select p " + "from Product p " + "where p.id = :productId", Product.class) .setParameter("productId", productId).getSingleResult(); assertNotNull(product); return null; } }); transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus transactionStatus) { LOG.info("Fetch using Criteria"); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> cq = cb.createQuery(Product.class); Root<Product> productRoot = cq.from(Product.class); cq.where(cb.equal(productRoot.get("id"), productId)); Product product = entityManager.createQuery(cq).getSingleResult(); assertNotNull(product); return null; } }); transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus transactionStatus) { LOG.info("Fetch using join fetch JPQL"); Product product = product = entityManager.createQuery("select p " + "from Product p " + "inner join fetch p.warehouseProductInfo " + "inner join fetch p.importer", Product.class) .getSingleResult(); assertNotNull(product); return null; } }); transactionTemplate.execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus transactionStatus) { Image image = entityManager .createQuery("select i " + "from Image i " + "inner join fetch i.product p " + "where p.id = :productId", Image.class) .setParameter("productId", productId).getResultList().get(0); assertNotNull(image); return null; } }); }