List of usage examples for javax.persistence.criteria CriteriaBuilder equal
Predicate equal(Expression<?> x, Object y);
From source file:com.moderndrummer.data.MemberDaoImpl.java
@Override public Member findMemberByUserName(String userName) { try {// www. j a v a 2s. c o m 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("name"), userName)); return em.createQuery(criteria).getSingleResult(); } catch (NoResultException e) { return new Member(); } }
From source file:org.sloth.persistence.impl.CategorieDaoImpl.java
@Override public Categorie getByTitle(String title) { if (title == null) { throw new NullPointerException(); }//ww w . jav a 2 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 w w . java 2 s.c o m */ public List<User> getCustomers() { 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)); return entityManager.createQuery(query).getResultList(); }
From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java
/** * {@inheritDoc}/*from ww w . ja va 2 s . co m*/ */ 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; }
From source file:com.netflix.genie.server.repository.jpa.ClusterSpecs.java
/** * Generate a specification given the parameters. * * @param name The name of the cluster to find * @param statuses The statuses of the clusters to find * @param tags The tags of the clusters to find * @param minUpdateTime The minimum updated time of the clusters to find * @param maxUpdateTime The maximum updated time of the clusters to find * @return The specification/*from w w w .j a v a 2 s .c o m*/ */ public static Specification<Cluster> find(final String name, final Set<ClusterStatus> statuses, final Set<String> tags, final Long minUpdateTime, final Long maxUpdateTime) { return new Specification<Cluster>() { @Override public Predicate toPredicate(final Root<Cluster> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(cb.like(root.get(Cluster_.name), name)); } if (minUpdateTime != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(Cluster_.updated), new Date(minUpdateTime))); } if (maxUpdateTime != null) { predicates.add(cb.lessThan(root.get(Cluster_.updated), new Date(maxUpdateTime))); } if (tags != null) { for (final String tag : tags) { if (StringUtils.isNotBlank(tag)) { predicates.add(cb.isMember(tag, root.get(Cluster_.tags))); } } } if (statuses != null && !statuses.isEmpty()) { //Could optimize this as we know size could use native array final List<Predicate> orPredicates = new ArrayList<>(); for (final ClusterStatus status : statuses) { orPredicates.add(cb.equal(root.get(Cluster_.status), status)); } predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()]))); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); } }; }
From source file:fi.vm.sade.organisaatio.dao.impl.YhteystietoElementtiDAOImpl.java
@Override public List<YhteystietoElementti> findByLisatietoIdAndKentanNimi(String yhteystietojenTyyppiOid, String kentanNimi) {/* www . java 2s. c o m*/ CriteriaBuilder cb = getEntityManager().getCriteriaBuilder(); CriteriaQuery<YhteystietoElementti> query = cb.createQuery(YhteystietoElementti.class); Root<YhteystietoElementti> root = query.from(YhteystietoElementti.class); query.select(root); Predicate tyyppiEquals = cb.equal(root.get("yhteystietojenTyyppi").get("oid"), yhteystietojenTyyppiOid); Predicate nameEquals = cb.equal(root.get("nimi"), kentanNimi); Predicate whereClause = cb.and(tyyppiEquals, nameEquals); query.where(whereClause); return getEntityManager().createQuery(query).getResultList(); // YhteystietojenTyyppi ytT = yttDao.findBy("oid", yhteystietojenTyyppiOid).get(0); // Query query = getEntityManager().createQuery( // "SELECT ltk FROM YhteystietoElementti ltk " + // "WHERE ltk.yhteystietojenTyyppi.id = :yhteystietojenTyyppiId " + // "AND ltk.nimi = :kentanNimi" // ); // return query // .setParameter("yhteystietojenTyyppiId", ytT.getId()) // .setParameter("kentanNimi", kentanNimi) // .getResultList(); }
From source file:org.osiam.resource_server.storage.dao.ExtensionDao.java
/** * Retrieves the extension with the given URN from the database * * @param urn//from w w w . ja va2s . co m * the URN of the extension to look up * @param caseInsensitive * should the case of the URN be ignored * @return the extension entity */ public ExtensionEntity getExtensionByUrn(String urn, boolean caseInsensitive) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<ExtensionEntity> cq = cb.createQuery(ExtensionEntity.class); Root<ExtensionEntity> extension = cq.from(ExtensionEntity.class); Predicate predicate; if (caseInsensitive) { predicate = cb.equal(cb.lower(extension.get(ExtensionEntity_.urn)), urn.toLowerCase(Locale.ENGLISH)); } else { predicate = cb.equal(extension.get(ExtensionEntity_.urn), urn); } cq.select(extension).where(predicate); TypedQuery<ExtensionEntity> query = em.createQuery(cq); ExtensionEntity singleExtension; try { singleExtension = query.getSingleResult(); } catch (NoResultException e) { throw new OsiamException("Could not find the Extension '" + urn + "'.", e); } return singleExtension; }
From source file:org.osiam.resource_server.storage.dao.ResourceDao.java
/** * Retrieves a single {@link ResourceEntity} by the given attribute and value. * /*from www .ja va 2s .c o m*/ * @param attribute * The attribute of the resource entity to retrieve it by * @param value * The value of the attribute to compare it to * @param clazz * The concrete resource entity class to retrieve (may also be {@link ResourceEntity}) * @return The matching {@link ResourceEntity} * @throws ResourceNotFoundException * If no {@link ResourceEntity} could be found * @throws OsiamException * If more than 1 {@link ResourceEntity} was found */ public <T extends ResourceEntity, V> T getByAttribute(SingularAttribute<? super T, V> attribute, V value, Class<T> clazz) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(clazz); Root<T> resource = cq.from(clazz); cq.select(resource).where(cb.equal(resource.get(attribute), value)); TypedQuery<T> q = em.createQuery(cq); try { return q.getSingleResult(); } catch (NoResultException nre) { throw new ResourceNotFoundException( String.format("Resource with attribute '%s' set to '%s' not found", attribute.getName(), value), nre); } catch (NonUniqueResultException nure) { throw new OsiamException(String.format("Muliple resources with attribute '%s' set to '%s' found", attribute.getName(), value), nure); } }
From source file:com.aimdek.ccm.dao.impl.UserRepositoryImpl.java
/** * {@inheritDoc}/*from w w w. java 2 s . 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.expressui.core.dao.security.query.RelatedPermissionsQuery.java
@Override public List<Predicate> buildCriteria(CriteriaBuilder builder, CriteriaQuery<Permission> query, Root<Permission> permission) { List<Predicate> predicates = new ArrayList<Predicate>(); if (hasValue(role)) { ParameterExpression<Role> roleExp = builder.parameter(Role.class, "role"); predicates.add(builder.equal(permission.get("role"), roleExp)); }/* ww w . j a v a 2s .c o m*/ return predicates; }