List of usage examples for javax.persistence.criteria CriteriaBuilder and
Predicate and(Predicate... restrictions);
From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java
/** * Find an entity of the given type, using as filter criteria the specified * list of attribute values. The filter criteria must uniquely identify an * entity.// w w w . ja v a2 s. c o m * * @param entityClass * @param params * @return * @throws BusinessException */ public <T> T findEntityByAttributes(Class<T> entityClass, Map<String, Object> params) { CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root); Assert.notNull(params); Predicate p = null; if (entityClass.isAssignableFrom(IModelWithClientId.class)) { p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId()); } for (Map.Entry<String, Object> entry : params.entrySet()) { p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue())); } cq.where(p); TypedQuery<T> query = this.getEntityManager().createQuery(cq); return (T) query.getSingleResult(); }
From source file:net.nan21.dnet.core.business.service.entity.AbstractEntityReadService.java
/** * Find a list of entities of the given type, using as filter criteria the * specified list of attribute values./*from w ww .j a va 2 s .c o m*/ * * @param entityClass * @param params * @return * @throws BusinessException */ public <T> List<T> findEntitiesByAttributes(Class<T> entityClass, Map<String, Object> params) { CriteriaBuilder cb = this.getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root); Assert.notNull(params); Predicate p = null; if (entityClass.isAssignableFrom(IModelWithClientId.class)) { p = cb.equal(root.get("clientId"), Session.user.get().getClient().getId()); } for (Map.Entry<String, Object> entry : params.entrySet()) { p = cb.and(cb.equal(root.get(entry.getKey()), entry.getValue())); } cq.where(p); TypedQuery<T> query = this.getEntityManager().createQuery(cq); return (List<T>) query.getResultList(); }
From source file:com.wms.studio.service.LoginIpServiceImpl.java
@Override @org.springframework.transaction.annotation.Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public PageDto<LoginIpInfoDto> findBy(final String userId, final Date start, final Date end, PageSize pageSize) {//from w w w .ja v a 2 s. com if (pageSize == null) { pageSize = new PageSize(); } return loginIpConvert.covertToDto(loginIpRepository.findAll(new Specification<LoginIp>() { @Override public Predicate toPredicate(Root<LoginIp> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> pres = new ArrayList<>(3); if (start != null) { pres.add(cb.greaterThanOrEqualTo(root.get("loginTime").as(Date.class), start)); } if (end != null) { pres.add(cb.lessThanOrEqualTo(root.get("loginTime").as(Date.class), end)); } if (!StringUtils.isBlank(userId)) { pres.add(cb.equal(root.get("user").as(User.class), new User(userId))); } Predicate[] p = new Predicate[pres.size()]; return cb.and(pres.toArray(p)); } }, new PageRequest(pageSize.getPage() - 1, pageSize.getLimit()))); }
From source file:it.attocchi.jpa2.JPAEntityFilter.java
/** * //www .ja v a2s. c o m * @param criteriaBuilder * @param paths * @return */ protected Predicate buildMultiWordLikePredicate(CriteriaBuilder criteriaBuilder, Path<String>... paths) { String[] words = semeRicerca.split(" "); List<Predicate> likeAllFields = new ArrayList<Predicate>(); for (Path<String> path : paths) { List<Predicate> likeOnWord = new ArrayList<Predicate>(); for (String word : words) { likeOnWord.add(criteriaBuilder.like(path, getForLike(word))); } Predicate p1 = criteriaBuilder.and(likeOnWord.toArray(new Predicate[likeOnWord.size()])); likeAllFields.add(p1); likeOnWord.clear(); } Predicate res = criteriaBuilder.or(likeAllFields.toArray(new Predicate[likeAllFields.size()])); return res; }
From source file:com.wms.studio.service.WallpaperServiveImpl.java
@Override @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true) public PageDto<WallpaperDto> findBy(final WallpaperEnum wallpaperType, final Date startDate, final Date endDate, PageSize pageSize) {//from w ww .jav a 2 s . c o m if (pageSize == null) { pageSize = new PageSize(); } Page<Wallpaper> pageWallpaper = this.wallpaperRepository.findAll(new Specification<Wallpaper>() { @Override public Predicate toPredicate(Root<Wallpaper> root, CriteriaQuery<?> query, CriteriaBuilder cb) { List<Predicate> pres = new ArrayList<Predicate>(); if (wallpaperType != null) { pres.add(cb.equal(root.get("wallpaperType").as(WallpaperEnum.class), wallpaperType)); } if (startDate != null) { pres.add(cb.greaterThanOrEqualTo(root.get("addDate").as(Date.class), startDate)); } if (endDate != null) { pres.add(cb.lessThanOrEqualTo(root.get("addDate").as(Date.class), endDate)); } Predicate[] p = new Predicate[pres.size()]; return cb.and(pres.toArray(p)); } }, new PageRequest(pageSize.getPage() - 1, pageSize.getLimit())); return this.wallpaperCovert.covertToDto(pageWallpaper); }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<ImageProductDTO> getImageProductDTOs() { return transactionTemplate.execute(new TransactionCallback<List<ImageProductDTO>>() { @Override/* w w w .j av a 2 s . c om*/ 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:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}//from ww w. j a v a 2s . com */ @Override public <T extends BaseEntity> List<T> findByCriteria(ReadCriteria<T> criteria) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); try { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(criteria.getEntity()); Root<T> root = cq.from(criteria.getEntity()); return em.createQuery(cq.where(cb.and(Predicates.from(criteria.getCriteriaAttributes(), cb, root)))) .getResultList(); } catch (Exception ex) { // NOSONAR throw new JpaException(ex); } finally { JpaUtil.closeEntityManager(em); } }
From source file:com.adeptj.modules.data.jpa.core.AbstractJpaRepository.java
/** * {@inheritDoc}// w ww . j a va 2s. co m */ @Override public <T extends BaseEntity> List<T> findPaginatedRecordsByCriteria(ReadCriteria<T> criteria) { EntityManager em = JpaUtil.createEntityManager(this.getEntityManagerFactory()); try { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(criteria.getEntity()); Root<T> root = cq.from(criteria.getEntity()); return em.createQuery(cq.where(cb.and(Predicates.from(criteria.getCriteriaAttributes(), cb, root)))) .setFirstResult(criteria.getStartPos()).setMaxResults(criteria.getMaxResult()).getResultList(); } catch (Exception ex) { // NOSONAR throw new JpaException(ex); } finally { JpaUtil.closeEntityManager(em); } }
From source file:com.vladmihalcea.HibernateCriteriaTest.java
private List<Product> getProducts_Mercifully() { return transactionTemplate.execute(new TransactionCallback<List<Product>>() { @Override//from w w w . ja v a 2 s . c o m public List<Product> doInTransaction(TransactionStatus transactionStatus) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<Product> query = cb.createQuery(Product.class); Root<Image> imageRoot = query.from(Image.class); Join<Image, Product> productJoin = imageRoot.join(Image_.product); query.select(productJoin); 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()]))); return entityManager.createQuery(query).getResultList(); } }); }
From source file:it.attocchi.jpa2.JPAEntityFilter.java
public Predicate getWherePredicate(Class<T> clazz, EntityManagerFactory emf) throws Exception { // CriteriaBuilder CriteriaBuilder criteriaBuilder = emf.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(clazz); Root<T> root = criteriaQuery.from(clazz); criteriaQuery.select(root);// www . ja va 2 s .com List<Predicate> predicateList = new ArrayList<Predicate>(); // return getWherePredicates(emf, predicateList, criteriaQuery, // criteriaBuilder, root); Predicate[] predicates = getWherePredicates(emf, predicateList, criteriaQuery, criteriaBuilder, root); Predicate wherePredicate = criteriaBuilder.and(predicates); return wherePredicate; }