List of usage examples for javax.persistence.criteria CriteriaQuery from
<X> Root<X> from(Class<X> entityClass);
From source file:fr.amapj.service.services.authentification.PasswordManager.java
/** * Retrouve l'utilisateur avec cet e-mail * Retourne null si non trouv ou autre problme *///from w w w . ja va 2 s . co m private Utilisateur findUser(String email, EntityManager em) { if ((email == null) || email.equals("")) { return null; } CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Utilisateur> cq = cb.createQuery(Utilisateur.class); Root<Utilisateur> root = cq.from(Utilisateur.class); // On ajoute la condition where cq.where(cb.equal(root.get(Utilisateur.P.EMAIL.prop()), email)); List<Utilisateur> us = em.createQuery(cq).getResultList(); if (us.size() == 0) { return null; } if (us.size() > 1) { logger.warn("Il y a plusieurs utilisateurs avec l'adresse " + email); return null; } return us.get(0); }
From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java
@Override @TransactionAttribute/*from w w w .j av a 2s . c o m*/ public List<T> findByFullTexts(final String terms, final OrderBy<T> orders, final long first, final long count, final String... fg) { LOGGER.debug("searching by text {} terms {}, fg {}", new Object[] { persistentClass, terms, fg }); if (settings.returnsNullIfTermsAreNull() && Strings.isNullOrEmpty(terms)) { return Collections.emptyList(); } if (fg != null) { entityManager.pushFetchPlan(); entityManager.getFetchPlan().addFetchGroups(fg); } CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<T> q = cb.createQuery(persistentClass); Root<T> p = q.from(persistentClass); q.where(newFullTextPredicate(cb, p, terms)); if (orders != null) { List<Order> orderList = orders.orders(cb, p); if (orderList != null && !orderList.isEmpty()) { q.orderBy(orderList); } } TypedQuery<T> query = entityManager.createQuery(q); OrmUtils.firstCount(query, first, count); List<T> resultList = query.getResultList(); if (fg != null) { entityManager.popFetchPlan(); } return resultList; }
From source file:com.aimdek.ccm.dao.impl.test.UsersDaoImplTest.java
/** * Test user like.//from www . j a va 2 s. c om */ @Test public void testUserLike() { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> root = query.from(User.class); query.where(builder.like(root.<String>get("lastName"), "test%")); List<User> user = entityManager.createQuery(query).getResultList(); assertEquals(10, user.size()); }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<Product> getProducts_Mercilessly() { return transactionTemplate.execute(new TransactionCallback<List<Product>>() { @Override/*from ww w . j av a2 s .co m*/ public List<Product> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> query = cb.createQuery(Product.class); Root<Product> product = query.from(Product.class); query.select(product); query.distinct(true); List<Predicate> criteria = new ArrayList<Predicate>(); criteria.add(cb.like(cb.lower(product.get(Product_.name)), "%tv%")); Subquery<Long> subQuery = query.subquery(Long.class); Root<Image> infoRoot = subQuery.from(Image.class); Join<Image, Product> productJoin = infoRoot.join(Image_.product); subQuery.select(productJoin.<Long>get(Product_.id)); subQuery.where(cb.gt(infoRoot.get(Image_.index), 0)); criteria.add(cb.in(product.get(Product_.id)).value(subQuery)); query.where(cb.and(criteria.toArray(new Predicate[criteria.size()]))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:org.businessmanager.dao.GenericDaoImpl.java
@Override public Long getCount(Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) { CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = queryBuilder.createQuery(Long.class); Root<T> rootQuery = criteriaQuery.from(getPersistenceClass()); CriteriaQuery<Long> select = criteriaQuery.select(queryBuilder.count(rootQuery)); List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder, rootQuery);//from ww w .j ava 2s. c o m criteriaQuery.where(predicateList.toArray(new Predicate[0])); TypedQuery<Long> typedQuery = entityManager.createQuery(select); return typedQuery.getSingleResult(); }
From source file:com.aimdek.ccm.dao.impl.test.UsersDaoImplTest.java
/** * Test retrieve limited user./*from w ww. jav a2 s. c om*/ */ @Test public void testRetrieveLimitedUser() { int limit = 10; int start = 5; CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> root = query.from(User.class); query.where(builder.equal(root.get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER)); List<User> user = entityManager.createQuery(query).setFirstResult(start).setMaxResults(limit) .getResultList(); assertEquals(user.size(), 5); }
From source file:com.sfs.captor.controller.ProductReleaseAction.java
/** * load product releases/*from w w w.j ava2s . com*/ */ private void loadList() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<ProductRelease> c = cb.createQuery(ProductRelease.class); Root<ProductRelease> obj = c.from(ProductRelease.class); c.select(obj).where(cb.equal(obj.get("project"), this.project)).orderBy(cb.asc(obj.get("id"))); this.productReleases = em.createQuery(c).getResultList(); }
From source file:fr.amapj.service.services.authentification.PasswordManager.java
/** * Retrouve l'utilisateur avec ce resetPasswordSald * Retourne null si non trouv ou autre problme *///ww w . j a va 2s.c o m @DbRead public Utilisateur findUserWithResetPassword(String resetPasswordSalt) { EntityManager em = TransactionHelper.getEm(); if ((resetPasswordSalt == null) || resetPasswordSalt.equals("")) { return null; } CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Utilisateur> cq = cb.createQuery(Utilisateur.class); Root<Utilisateur> root = cq.from(Utilisateur.class); // On ajoute la condition where cq.where(cb.equal(root.get(Utilisateur.P.RESETPASSWORDSALT.prop()), resetPasswordSalt)); List<Utilisateur> us = em.createQuery(cq).getResultList(); if (us.size() == 0) { return null; } if (us.size() > 1) { logger.warn("Il y a plusieurs utilisateurs avec le salt " + resetPasswordSalt); return null; } Utilisateur u = us.get(0); if (u.getEtatUtilisateur() == EtatUtilisateur.INACTIF) { return null; } return u; }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<ImageProductDTO> getImageProductDTOs() { return transactionTemplate.execute(new TransactionCallback<List<ImageProductDTO>>() { @Override//from ww w . ja va 2s . c o m public List<ImageProductDTO> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ImageProductDTO> query = cb.createQuery(ImageProductDTO.class); Root<Image> imageRoot = query.from(Image.class); Join<Image, Product> productJoin = imageRoot.join(Image_.product); query.distinct(true); List<Predicate> criteria = new ArrayList<Predicate>(); criteria.add(cb.like(cb.lower(productJoin.get(Product_.name)), "%tv%")); criteria.add(cb.gt(imageRoot.get(Image_.index), 0)); query.where(cb.and(criteria.toArray(new Predicate[criteria.size()]))); query.select(cb.construct(ImageProductDTO.class, imageRoot.get(Image_.name), productJoin.get(Product_.name))).orderBy(cb.asc(imageRoot.get(Image_.name))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:org.businessmanager.dao.GenericDaoImpl.java
public List<T> findByAssignedEntity(ListAttribute<T, ?> listAttribute, Long entityId) { CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass()); Root<T> rootQuery = criteriaQuery.from(getPersistenceClass()); CriteriaQuery<T> select = criteriaQuery.select(rootQuery); Join<T, ?> memberJoin = rootQuery.join(listAttribute); Path<?> nameField = memberJoin.get("id"); Predicate nameEquals = queryBuilder.equal(nameField, entityId); criteriaQuery.where(nameEquals);// w ww . j a v a 2 s. c o m TypedQuery<T> typedQuery = getEntityManager().createQuery(select); return typedQuery.getResultList(); }