List of usage examples for javax.persistence.criteria CriteriaBuilder equal
Predicate equal(Expression<?> x, Object y);
From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java
@Override @TransactionAttribute/* w w w. j ava 2s . co m*/ public long countByAttribute(String attributeName, Object attributeValue) { 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(cb.equal(p.get(attributeName), attributeValue)); try { return entityManager.createQuery(q).getSingleResult(); } catch (NoResultException e) { return 0; } }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private Product getProduct_Mercilessly() { return transactionTemplate.execute(new TransactionCallback<Product>() { @Override/*from w ww. j ava2 s.c o m*/ public Product doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> query = cb.createQuery(Product.class); Root<Product> productRoot = query.from(Product.class); query.select(productRoot) .where(cb.and(cb.equal(productRoot.get(Product_.code), "tvCode"), cb.gt( productRoot.get(Product_.warehouseProductInfo).get(WarehouseProductInfo_.quantity), 50))); return entityManager.createQuery(query).getSingleResult(); } }); }
From source file:net.echinopsii.ariane.community.plugin.rabbitmq.directory.RabbitmqDirectoryBootstrap.java
private void plugDirectoryJPAProvider() { Company pivotal = null;/* w ww .jav a2 s .c o m*/ Application rabbitmq = null; directoryJpaProvider.addSubPersistenceBundle(FrameworkUtil.getBundle(RabbitmqDirectoryBootstrap.class)); EntityManager em = directoryJpaProvider.createEM(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Company> cmpCriteria = builder.createQuery(Company.class); Root<Company> cmpRoot = cmpCriteria.from(Company.class); cmpCriteria.select(cmpRoot).where(builder.equal(cmpRoot.<String>get("name"), "Pivotal")); TypedQuery<Company> cmpQuery = em.createQuery(cmpCriteria); try { pivotal = cmpQuery.getSingleResult(); log.debug("Pivotal company already defined ..."); } catch (NoResultException e) { log.debug("Pivotal company will be defined ..."); } catch (Exception e) { throw e; } CriteriaQuery<Application> appCriteria = builder.createQuery(Application.class); Root<Application> appRoot = appCriteria.from(Application.class); appCriteria.select(appRoot).where(builder.equal(appRoot.<String>get("name"), "RabbitMQ")); TypedQuery<Application> appQuery = em.createQuery(appCriteria); try { rabbitmq = appQuery.getSingleResult(); log.debug("RabbitMQ application already defined ..."); } catch (NoResultException e) { log.debug("RabbitMQ application will be defined ..."); } catch (Exception e) { throw e; } em.getTransaction().begin(); if (pivotal == null) { pivotal = new Company().setNameR("Pivotal").setDescriptionR("Pivotal"); em.persist(pivotal); } if (rabbitmq == null) { rabbitmq = new Application().setNameR("RabbitMQ").setCompanyR(pivotal).setShortNameR("RabbitMQ") .setColorCodeR("ff6600").setDescriptionR("Robust messaging for applications"); em.persist(rabbitmq); } if (!pivotal.getApplications().contains(rabbitmq)) { pivotal.getApplications().add(rabbitmq); } em.flush(); em.getTransaction().commit(); }
From source file:ch.puzzle.itc.mobiliar.business.resourcegroup.control.ResourceGroupRepository.java
/** * * @param name// w w w. j a va 2 s .c o m * @param resourceTypeId * @return */ public ResourceGroupEntity loadUniqueGroupByNameAndType(String name, Integer resourceTypeId) { ResourceGroupEntity result = null; try { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ResourceGroupEntity> q = cb.createQuery(ResourceGroupEntity.class); Root<ResourceGroupEntity> r = q.from(ResourceGroupEntity.class); r.fetch("resources"); Join<ResourceGroupEntity, ResourceEntity> resources = r.join("resources"); Predicate typePred = cb.equal(resources.get("resourceType").get("id"), resourceTypeId); Predicate resNamePred = cb.equal(resources.get("name"), name); q.where(cb.and(typePred, resNamePred)); q.distinct(true); result = entityManager.createQuery(q).getSingleResult(); } catch (NoResultException e) { // do nothing } return result; }
From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java
/** * Retrieves the distinct UUIDs of the available requesting facilities. *///from w w w. j a v a 2 s . com @Override public List<UUID> getRequestingFacilities(List<UUID> supplyingFacilityIds) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<UUID> query = builder.createQuery(UUID.class); Root<Order> root = query.from(Order.class); if (!isEmpty(supplyingFacilityIds)) { Predicate predicate = builder.disjunction(); for (Object elem : supplyingFacilityIds) { predicate = builder.or(predicate, builder.equal(root.get(SUPPLYING_FACILITY_ID), elem)); } query.where(predicate); } query.select(root.get(REQUESTING_FACILITY_ID)).distinct(true); return entityManager.createQuery(query).getResultList(); }
From source file:com.aimdek.ccm.dao.impl.TransactionRepositoryImpl.java
/** * {@inheritDoc}//from w ww.j a va 2 s . c o m */ public List<Transaction> findTransactionsByStatementDateAndCreditCardId(Date startDate, Date endDate, long creditCardId) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Transaction> query = builder.createQuery(Transaction.class); Root<Transaction> root = query.from(Transaction.class); query.select(root); Predicate date = builder.greaterThan(root.<Date>get(FIELD_CONSTANT_TRANSACTION_DATE), startDate); Predicate isCreditCardId = builder.equal(root.<String>get(FIELD_CONSTANT_CREDIT_CARD_ID), creditCardId); query.where(builder.and(date, isCreditCardId)); return super.find(query); }
From source file:net.groupbuy.dao.impl.MessageDaoImpl.java
public Page<Message> findDraftPage(Member sender, Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Message> criteriaQuery = criteriaBuilder.createQuery(Message.class); Root<Message> root = criteriaQuery.from(Message.class); criteriaQuery.select(root);//from w w w .ja v a2 s . co m Predicate restrictions = criteriaBuilder.conjunction(); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("forMessage")), criteriaBuilder.equal(root.get("isDraft"), true)); if (sender != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("sender"), sender)); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNull(root.get("sender"))); } criteriaQuery.where(restrictions); return super.findPage(criteriaQuery, pageable); }
From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java
/** * Find an entity of the given type, using as filter criteria the specified * list of attribute values. The filter criteria must uniquely identify an * entity.//from w w w. j a v a2 s. com * * @param entityClass * @param params * @return * @throws BusinessException */ public <T> T findEntityByAttributes(Class<T> entityClass, Map<String, Object> params) { CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root); Assert.notNull(params); Predicate p = null; if (entityClass.isAssignableFrom(IModelWithClientId.class)) { p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId()); } for (Map.Entry<String, Object> entry : params.entrySet()) { p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue())); } cq.where(p); TypedQuery<T> query = this.getEntityManager().createQuery(cq); return (T) query.getSingleResult(); }
From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java
/** * Find a list of entities of the given type, using as filter criteria the * specified list of attribute values./*w w w. j a v a2 s. co m*/ * * @param entityClass * @param params * @return * @throws BusinessException */ public <T> List<T> findEntitiesByAttributes(Class<T> entityClass, Map<String, Object> params) { CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root); Assert.notNull(params); Predicate p = null; if (entityClass.isAssignableFrom(IModelWithClientId.class)) { p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId()); } for (Map.Entry<String, Object> entry : params.entrySet()) { p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue())); } cq.where(p); TypedQuery<T> query = this.getEntityManager().createQuery(cq); return (List<T>) query.getResultList(); }
From source file:net.shopxx.dao.impl.CouponCodeDaoImpl.java
public Long count(Coupon coupon, Member member, Boolean hasBegun, Boolean hasExpired, Boolean isUsed) { 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 a2 s . co m*/ Predicate restrictions = criteriaBuilder.conjunction(); Path<Coupon> couponPath = root.get("coupon"); if (coupon != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(couponPath, coupon)); } if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (hasBegun != null) { if (hasBegun) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(couponPath.get("beginDate").isNull(), criteriaBuilder.lessThanOrEqualTo(couponPath.<Date>get("beginDate"), new Date()))); } else { restrictions = criteriaBuilder.and(restrictions, couponPath.get("beginDate").isNotNull(), criteriaBuilder.greaterThan(couponPath.<Date>get("beginDate"), new Date())); } } if (hasExpired != null) { if (hasExpired) { restrictions = criteriaBuilder.and(restrictions, couponPath.get("endDate").isNotNull(), criteriaBuilder.lessThanOrEqualTo(couponPath.<Date>get("endDate"), new Date())); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(couponPath.get("endDate").isNull(), criteriaBuilder.greaterThan(couponPath.<Date>get("endDate"), new Date()))); } } if (isUsed != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isUsed"), isUsed)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }