List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery
<T> CriteriaQuery<T> createQuery(Class<T> resultClass);
CriteriaQuery
object with the specified result type. From source file:org.sloth.persistence.impl.ReportDaoImpl.java
@Override public Collection<Report> getByUser(User u) throws NullPointerException, IllegalArgumentException { if (u == null) { throw new NullPointerException(); }//ww w.j av a 2 s . c om if (u.isNew()) { throw new IllegalArgumentException(); } CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Report> cq = cb.createQuery(Report.class); Root<Report> r = cq.from(Report.class); cq.select(r).where(cb.equal(r.get(Report_.author), u)); Collection<Report> result = getEntityManager().createQuery(cq).getResultList(); logger.info("Found {} Reports by {}.", result.size(), u); return result; }
From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java
/** * {@inheritDoc}// w ww .j a v 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:org.sloth.persistence.impl.ReportDaoImpl.java
@Override public Collection<Report> getByObservation(Observation o) throws NullPointerException, IllegalArgumentException { if (o == null) { throw new NullPointerException(); }/*from w ww.j a v a2s .c om*/ if (o.isNew()) { throw new IllegalArgumentException(); } CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<Report> cq = cb.createQuery(Report.class); Root<Report> r = cq.from(Report.class); cq.select(r).where(cb.equal(r.get(Report_.observation), o)); Collection<Report> result = getEntityManager().createQuery(cq).getResultList(); logger.info("{} Reports for Observation {}.", result.size(), o); return result; }
From source file:net.groupbuy.dao.impl.ReviewDaoImpl.java
public Page<Review> findPage(Member member, Product product, Type type, Boolean isShow, Pageable pageable) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class); Root<Review> root = criteriaQuery.from(Review.class); criteriaQuery.select(root);/*from ww w . j a va2 s. c o m*/ Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (product != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product)); } if (type == Type.positive) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4)); } else if (type == Type.moderate) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3)); } else if (type == Type.negative) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2)); } if (isShow != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow)); } criteriaQuery.where(restrictions); return super.findPage(criteriaQuery, pageable); }
From source file:net.groupbuy.dao.impl.ReviewDaoImpl.java
public Long count(Member member, Product product, Type type, Boolean isShow) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class); Root<Review> root = criteriaQuery.from(Review.class); criteriaQuery.select(root);/*from ww w . j a v a2 s . co m*/ Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (product != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product)); } if (type == Type.positive) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4)); } else if (type == Type.moderate) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3)); } else if (type == Type.negative) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2)); } if (isShow != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow)); } criteriaQuery.where(restrictions); return super.count(criteriaQuery, null); }
From source file:controllers.AuthController.java
@UnitOfWork @FilterWith(CTCheck.class) public Result login(Context context, Session session, User req) { EntityManager entitymanager = entitiyManagerProvider.get(); CriteriaBuilder cb = entitymanager.getCriteriaBuilder(); try {//from ww w . jav a 2s. c o m CriteriaQuery<User> query = cb.createQuery(User.class); Root<User> a = query.from(User.class); query.where(cb.equal(a.get(User_.email), req.getEmail()), cb.equal(a.get(User_.password), req.getPassword())); User u = entitymanager.createQuery(query).getSingleResult(); String token = SessionIdentifierGenerator.nextSessionId(); context.addCookie(Cookie.builder("token", token).build()); session.put("token", token); session.put("email", u.getEmail()); session.put("id", String.valueOf(u.getId())); ninjaCache.set(token, u.getId()); return Results.json().render(new RespAuth(u.getId(), token, u.getEmail())); } catch (Exception e) { e.printStackTrace(); return Results.json().status(400).render(new JSendResp(400, e)); } }
From source file:ispok.dao.TournamentHibernateJpaDao.java
@Override public List<Tournament> getPage(int first, int rows, String sortBy, boolean ascending, Map<String, Object> filters) { // Session session = getEntityManager().unwrap(Session.class); // Criteria criteria = session.createCriteria(Tournament.class); // CriteriaBuilder cb = entityManagerfactory.getCriteriaBuilder(); // CriteriaQuery<Tournament> cq = cb.createQuery(Tournament.class); // Root<Tournament> tou = cq.from(Tournament.class); // cb.para // criteria.add(cb.equal(tou, tou)) // String queryString = "SELECT e FROM " + Tournament.class.getSimpleName(); // if (!filters.isEmpty()) { // queryString += " WHERE "; // for (Map.Entry<String, Object> entry : filters.entrySet()) { // if ("id".equals(entry.getKey())) { // queryString += "e." + entry.getKey() + " = " + entry.getValue(); // } else { // queryString += "e." + entry.getKey() + " LIKE '" + entry.getValue() + "'"; // } // } // }//from w w w . j a va2 s . c om // " e WHERE e." + property + " = :value"; // return getEntityManager().createQuery(queryString).setParameter("value", value).getResultList(); // return null; // Session session = getEntityManager().unwrap(Session.class); // Criteria criteria = session.createCriteria(Tournament.class); // // for (Map.Entry<String, Object> filter : filters.entrySet()) { // if ("id".equals(filter.getKey())) { // criteria.add(Restrictions.eq("id", Long.parseLong(filter.getValue().toString()))); // } else { // criteria.add(Restrictions.ilike(filter.getKey(), (String) filter.getValue(), MatchMode.START)); // } // } // // if (ascending) { // criteria.addOrder(Order.asc(sortBy)); // } else { // criteria.addOrder(Order.desc(sortBy)); // } // // criteria.setFirstResult(first); // criteria.setMaxResults(rows); // // criteria.setResultTransformer(Transformers.aliasToBean(clazz)); // List<ENTITY> list = criteria.list(); // // return list; EntityManager em = getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tournament> cq = cb.createQuery(Tournament.class); Root<Tournament> t = cq.from(Tournament.class); cq.select(t); TypedQuery<Tournament> tq = em.createQuery(cq); List<Tournament> tournaments = tq.getResultList(); return tournaments; }
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 a 2s . com*/ 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.groupbuy.dao.impl.ReviewDaoImpl.java
public List<Review> findList(Member member, Product product, Type type, Boolean isShow, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Review> criteriaQuery = criteriaBuilder.createQuery(Review.class); Root<Review> root = criteriaQuery.from(Review.class); criteriaQuery.select(root);/*from w w w . j a v a2 s.com*/ Predicate restrictions = criteriaBuilder.conjunction(); if (member != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member)); } if (product != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("product"), product)); } if (type == Type.positive) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.ge(root.<Number>get("score"), 4)); } else if (type == Type.moderate) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.<Number>get("score"), 3)); } else if (type == Type.negative) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.le(root.<Number>get("score"), 2)); } if (isShow != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isShow"), isShow)); } criteriaQuery.where(restrictions); return super.findList(criteriaQuery, null, count, filters, orders); }
From source file:net.shopxx.dao.impl.ArticleDaoImpl.java
public List<Article> findList(ArticleCategory articleCategory, Tag tag, Boolean isPublication, Integer count, List<Filter> filters, List<Order> orders) { CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Article> criteriaQuery = criteriaBuilder.createQuery(Article.class); Root<Article> root = criteriaQuery.from(Article.class); criteriaQuery.select(root);/* w w w. j av a2 s .c om*/ Predicate restrictions = criteriaBuilder.conjunction(); if (articleCategory != null) { Subquery<ArticleCategory> subquery = criteriaQuery.subquery(ArticleCategory.class); Root<ArticleCategory> subqueryRoot = subquery.from(ArticleCategory.class); subquery.select(subqueryRoot); subquery.where(criteriaBuilder.or(criteriaBuilder.equal(subqueryRoot, articleCategory), criteriaBuilder.like(subqueryRoot.<String>get("treePath"), "%" + ArticleCategory.TREE_PATH_SEPARATOR + articleCategory.getId() + ArticleCategory.TREE_PATH_SEPARATOR + "%"))); restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.in(root.get("articleCategory")).value(subquery)); } if (tag != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.join("tags"), tag)); } if (isPublication != null) { restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("isPublication"), isPublication)); } criteriaQuery.where(restrictions); if (orders == null || orders.isEmpty()) { criteriaQuery.orderBy(criteriaBuilder.desc(root.get("isTop")), criteriaBuilder.desc(root.get("createDate"))); } return super.findList(criteriaQuery, null, count, filters, orders); }