List of usage examples for javax.persistence.criteria CriteriaQuery select
CriteriaQuery<T> select(Selection<? extends T> selection);
From source file:com.moderndrummer.data.MemberDaoImpl.java
@Override public Member findByEmail(String email) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Member> criteria = builder.createQuery(Member.class); Root<Member> member = criteria.from(Member.class); criteria.select(member).where(builder.equal(member.get("email"), email)); return em.createQuery(criteria).getSingleResult(); }
From source file:com.orangeandbronze.jblubble.sample.PersonController.java
protected List<Person> getAllPersons() { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Person> criteriaQuery = criteriaBuilder.createQuery(Person.class); criteriaQuery.select(criteriaQuery.from(Person.class)); return entityManager.createQuery(criteriaQuery).getResultList(); }
From source file:org.sloth.persistence.impl.CategorieDaoImpl.java
@Override public Categorie getByTitle(String title) { if (title == null) { throw new NullPointerException(); }/*from w w w . ja va2 s .c o m*/ CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Categorie> cq = cb.createQuery(Categorie.class); Root<Categorie> categorie = cq.from(Categorie.class); cq.select(categorie); cq.where(cb.equal(categorie.get(Categorie_.title), title)); Categorie result = null; try { result = getEntityManager().createQuery(cq).getSingleResult(); } catch (NoResultException e) { logger.info("Categorie with Title {} not found", title); } catch (NonUniqueResultException e) { logger.warn("Corrupt Database", e); } return result; }
From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java
/** * {@inheritDoc}//from w ww . j av a 2s .c o m */ public long getCustomersCount(Map<String, Object> filters) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<User> root = query.from(User.class); query.select(builder.count(root)); query.where(builder.equal(root.<String>get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER)); this.addFilterCriteria(filters, builder, root, query); return entityManager.createQuery(query).getSingleResult(); }
From source file:com.aimdek.ccm.dao.impl.CreditCardRepositoryImpl.java
/** * {@inheritDoc}/*from ww w .j a v a2 s .c o m*/ */ public long getCreditCardsCount(Map<String, Object> filters) { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> query = builder.createQuery(Long.class); Root<CreditCard> root = query.from(CreditCard.class); query.select(builder.count(root)); addFilterCriteria(filters, builder, root, query); return entityManager.createQuery(query).getSingleResult(); }
From source file:com.aimdek.ccm.dao.impl.test.StatementDaoImplTest.java
/** * Test find filter statement.// www.j ava 2s . c o m */ @Test public void testFindFilterStatement() { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Statement> query = builder.createQuery(Statement.class); Root<Statement> root = query.from(Statement.class); query.select(root); int limit = 4 - 2; List<Statement> statementList = entityManager.createQuery(query).setFirstResult(2).setMaxResults(limit) .getResultList(); assertEquals(2, statementList.size()); }
From source file:com.aimdek.ccm.dao.impl.test.StatementDaoImplTest.java
/** * Test filtering./* w w w.java2 s . c o m*/ */ @Test public void testFiltering() { String cardNumber = "0008"; CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<Statement> query = builder.createQuery(Statement.class); Root<Statement> root = query.from(Statement.class); query.select(root); query.where(builder.like(root.<String>get(FIELDCONSTANT_CARDNUMBER), "%" + cardNumber)); List<Statement> statementList = entityManager.createQuery(query).getResultList(); assertEquals(4, statementList.size()); }
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); Predicate restrictions = criteriaBuilder.conjunction(); Path<Coupon> couponPath = root.get("coupon"); if (coupon != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(couponPath, coupon)); }//w w w . j a v a 2 s . c o m 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); }
From source file:net.shopxx.dao.impl.ProductNotifyDaoImpl.java
public Long count(Member member, Boolean isMarketable, Boolean isOutOfStock, Boolean hasSent) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<ProductNotify> criteriaQuery = criteriaBuilder.createQuery(ProductNotify.class); Root<ProductNotify> root = criteriaQuery.from(ProductNotify.class); criteriaQuery.select(root); Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); }/*from www.j av a 2 s .c o m*/ if (isMarketable != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product").get("goods").get("isMarketable"), isMarketable)); } if (isOutOfStock != null) { Path<Integer> stock = root.get("product").get("stock"); Path<Integer> allocatedStock = root.get("product").get("allocatedStock"); if (isOutOfStock) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.isNotNull(stock), criteriaBuilder.lessThanOrEqualTo(stock, allocatedStock)); } else { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(criteriaBuilder.isNull(stock), criteriaBuilder.greaterThan(stock, allocatedStock))); } } if (hasSent != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("hasSent"), hasSent)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java
/** * {@inheritDoc}/*from www . j a v a 2 s . com*/ */ public User findLastCustomer() { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> root = query.from(User.class); query.select(root); query.where(builder.equal(root.<String>get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER)); query.orderBy(builder.desc(root.get(FIELD_CONSTANT_CREATED_AT))); try { return entityManager.createQuery(query).setMaxResults(1).getSingleResult(); } catch (NoResultException e) { LOGGER.error("Error while retrieving last customer", e); } return null; }