List of usage examples for javax.persistence.criteria CriteriaBuilder equal
Predicate equal(Expression<?> x, Object y);
From source file:com.shz.foundation.service.dynamic.DynamicSpecifications.java
public static <T> Specification<T> bySearchFilter(final Collection<SearchFilter> filters, final Class<T> entityClazz) { return new Specification<T>() { @Override//from www .j a va 2 s.co m public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) { if (Collections3.isNotEmpty(filters)) { List<Predicate> predicates = Lists.newArrayList(); for (SearchFilter filter : filters) { // nested path translate, Task??"user.name"filedName, ?Task.user.name String[] names = StringUtils.split(filter.fieldName, "."); Path expression = root.get(names[0]); for (int i = 1; i < names.length; i++) { expression = expression.get(names[i]); } // logic operator switch (filter.operator) { case EQ: //?? Object value = filter.value; if (isBoolean(value)) { value = Boolean.parseBoolean(value.toString()); } predicates.add(builder.equal(expression, value)); break; case LIKE: predicates.add(builder.like(expression, "%" + filter.value + "%")); break; case GT: predicates.add(builder.greaterThan(expression, (Comparable) filter.value)); break; case LT: predicates.add(builder.lessThan(expression, (Comparable) filter.value)); break; case GTE: predicates.add(builder.greaterThanOrEqualTo(expression, (Comparable) filter.value)); break; case LTE: predicates.add(builder.lessThanOrEqualTo(expression, (Comparable) filter.value)); break; } } // ? and ??? if (!predicates.isEmpty()) { return builder.and(predicates.toArray(new Predicate[predicates.size()])); } } return builder.conjunction(); } private boolean isBoolean(Object value) { String valueString = value.toString().toLowerCase(); if (valueString.equals("true") || valueString.equals("false")) return true; return false; } }; }
From source file:com.netflix.genie.core.jpa.specifications.JpaClusterSpecs.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// w w w . j a v a 2 s .c o m */ public static Specification<ClusterEntity> find(final String name, final Set<ClusterStatus> statuses, final Set<String> tags, final Date minUpdateTime, final Date maxUpdateTime) { return (final Root<ClusterEntity> root, final CriteriaQuery<?> cq, final CriteriaBuilder cb) -> { final List<Predicate> predicates = new ArrayList<>(); if (StringUtils.isNotBlank(name)) { predicates.add(JpaSpecificationUtils.getStringLikeOrEqualPredicate(cb, root.get(ClusterEntity_.name), name)); } if (minUpdateTime != null) { predicates.add(cb.greaterThanOrEqualTo(root.get(ClusterEntity_.updated), minUpdateTime)); } if (maxUpdateTime != null) { predicates.add(cb.lessThan(root.get(ClusterEntity_.updated), maxUpdateTime)); } if (tags != null && !tags.isEmpty()) { predicates .add(cb.like(root.get(ClusterEntity_.tags), JpaSpecificationUtils.getTagLikeString(tags))); } if (statuses != null && !statuses.isEmpty()) { //Could optimize this as we know size could use native array final List<Predicate> orPredicates = statuses.stream() .map(status -> cb.equal(root.get(ClusterEntity_.status), status)) .collect(Collectors.toList()); predicates.add(cb.or(orPredicates.toArray(new Predicate[orPredicates.size()]))); } return cb.and(predicates.toArray(new Predicate[predicates.size()])); }; }
From source file:com.excilys.ebi.sample.jpa.query.benchmark.repository.impl.JPACriteriaRepository.java
@Override public List<Song> getSongsByArtistNameOrderBySongTitle(final String name) { CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Song> query = builder.createQuery(Song.class); Root<Song> root = query.from(Song.class); query.where(builder.equal(root.get(Song_.artist).get(Artist_.name), name)) .orderBy(builder.asc(root.get(Song_.title))); return em.createQuery(query).getResultList(); }
From source file:com.ttech.cordovabuild.domain.user.UserRepositoryImpl.java
@Override public User findUserByEmail(String email) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> cq = cb.createQuery(User.class); Root<User> root = cq.from(User.class); return em.createQuery(cq.select(root).where(cb.equal(root.get(User_.email), email))).getSingleResult(); }
From source file:com.ttech.cordovabuild.domain.user.UserRepositoryImpl.java
@Override public User findUserByUserName(String username) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> cq = cb.createQuery(User.class); Root<User> root = cq.from(User.class); return em.createQuery(cq.select(root).where(cb.equal(root.get(User_.username), username))) .getSingleResult();//from www .java 2 s. c o m }
From source file:cz.muni.expense.data.UserRepository.java
public User findByUsername(String username) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> criteria = cb.createQuery(User.class); Root<User> user = criteria.from(User.class); criteria.select(user).where(cb.equal(user.get(User_.username), username)); try {/*from w w w.j a va 2 s. c o m*/ return em.createQuery(criteria).getSingleResult(); } catch (NoResultException ex) { return null; } }
From source file:dao.jpa.JpaUtilsTest.java
@Test @Transactional/*from w ww . ja v a2 s. c om*/ public void testInitialize() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Category> c = cb.createQuery(Category.class); Root<Category> root = c.from(Category.class); c.where(cb.equal(JpaUtils.getPath(root, "name"), "Java")); List<Category> list = em.createQuery(c).getResultList(); Category cat = list.get(0); JpaUtils.initialize(em, cat, 2); cat.getBooks().contains(new Book()); }
From source file:cz.muni.expense.data.UserRepository.java
public User findByUsernameAndAuthToken(String authId, String authToken) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<User> criteria = cb.createQuery(User.class); Root<User> user = criteria.from(User.class); criteria.select(user).where(cb.equal(user.get(User_.username), authId)); try {/* w w w. j av a2 s .c o m*/ User userToReturn = em.createQuery(criteria).getSingleResult(); return userToReturn.getAuthToken().equals(authToken) ? userToReturn : null; } catch (NoResultException ex) { return null; } }
From source file:org.openlmis.fulfillment.repository.custom.impl.OrderRepositoryImpl.java
private Predicate isEqual(String field, Object value, Root<Order> root, Predicate predicate, CriteriaBuilder builder) { return value != null ? builder.and(predicate, builder.equal(root.get(field), value)) : predicate; }
From source file:dao.jpa.JpaUtilsTest.java
@Test @Transactional/*from ww w.j av a 2s . c o m*/ public void testCountFromCriteria() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Book> bookQuery = cb.createQuery(Book.class); Root<Book> root = bookQuery.from(Book.class); bookQuery.where(cb.equal(JpaUtils.getPath(root, "author.name"), "Rod")); CriteriaQuery<Long> countQuery = JpaUtils.countCriteria(em, bookQuery); Long result = Long.valueOf(em.createQuery(bookQuery).getResultList().size()); assertEquals(result, (Long) em.createQuery(countQuery).getSingleResult()); }