Example usage for javax.persistence.criteria CriteriaQuery from

List of usage examples for javax.persistence.criteria CriteriaQuery from

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaQuery from.

Prototype

<X> Root<X> from(Class<X> entityClass);

Source Link

Document

Create and add a query root corresponding to the given entity, forming a cartesian product with any existing roots.

Usage

From source file:se.inera.intyg.intygstjanst.persistence.model.dao.impl.CertificateDaoImpl.java

@Override
public List<Certificate> findCertificate(Personnummer civicRegistrationNumber, List<String> types,
        LocalDate fromDate, LocalDate toDate, List<String> careUnits) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Certificate> query = criteriaBuilder.createQuery(Certificate.class);
    Root<Certificate> root = query.from(Certificate.class);

    root.fetch("states", JoinType.LEFT);

    if (civicRegistrationNumber == null) {
        return Collections.emptyList();
    }/*from   w w  w  .  jav a 2 s.com*/

    List<Predicate> predicates = new ArrayList<>();

    // meta data has to match civic registration number
    predicates.add(criteriaBuilder.equal(root.get("civicRegistrationNumber"),
            DaoUtil.formatPnrForPersistence(civicRegistrationNumber)));

    // filter by certificate types
    if (types != null && !types.isEmpty()) {
        predicates.add(criteriaBuilder.lower(root.<String>get("type")).in(toLowerCase(types)));
    }

    // filter by care unit
    if (careUnits != null && !careUnits.isEmpty()) {
        predicates.add(root.<String>get("careUnitId").in(careUnits));
    }

    query.where(predicates.toArray(new Predicate[predicates.size()]));

    // order by signed date
    query.orderBy(criteriaBuilder.asc(root.get("signedDate")));

    List<Certificate> tmpResult = entityManager.createQuery(query).getResultList();
    List<Certificate> result = filterDuplicates(tmpResult);

    // expect a small number, so lets filter in memory
    return new DateFilter(result).filter(fromDate, toDate);
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.UserSessionUrlDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.findAllUserSessionUrl = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<UserSessionUrlImpl>>() {
                @Override//from  w  w  w.  java 2  s .  c  o  m
                public CriteriaQuery<UserSessionUrlImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<UserSessionUrlImpl> criteriaQuery = cb
                            .createQuery(UserSessionUrlImpl.class);
                    final Root<UserSessionUrlImpl> definitionRoot = criteriaQuery
                            .from(UserSessionUrlImpl.class);
                    criteriaQuery.select(definitionRoot);

                    return criteriaQuery;
                }
            });

}

From source file:utils.jpa.EntityResource.java

private List<T> getAllPriv(@BeanParam TableSearchQuery tb, List<String> fieldsSearchBy) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<T> cq = cb.createQuery(entityClass);
    Root<T> root = cq.from(entityClass);

    cq.select(root);/*w  w  w  . ja v a2 s .  c o  m*/

    //setting order
    if (tb.getOrderType().equals(OrderType.DESC)) {
        cq.orderBy(cb.desc(root.get(tb.getOrder())));
    } else {
        cq.orderBy(cb.asc(root.get(tb.getOrder())));
    }

    Query query = tb.createQuery(em, cb, cq, root);
    if (query == null) {
        query = createCommonQuery(cb, cq, root, tb, fieldsSearchBy);
    }

    if (tb.getOffset() > 0) {
        query.setFirstResult(tb.getOffset());
    }
    if (tb.getLimit() > 0) {
        query.setMaxResults(tb.getLimit());
    }

    return query.getResultList();
}

From source file:de.hopmann.repositories.cran.service.CRANPackageListingService.java

public void removeCurrentListing() {
    // TODO Bulk removal, JPQL not cascading
    // entityManager.createQuery("DELETE FROM DependencyEntity").executeUpdate();
    // or/*from  www  .j  a v  a  2s  .c o m*/
    // entityManager.createQuery("DELETE FROM PackageEntity").executeUpdate();

    // Alternative

    // CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    //
    // CriteriaDelete<PackageEntity> delete = cb
    // .createCriteriaDelete(PackageEntity.class);
    // Root<PackageEntity> p = delete.from(PackageEntity.class);
    // delete.where(cb.isFalse(p.get(PackageEntity_.archived)));
    //
    // entityManager.createQuery(delete).executeUpdate();

    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<CRANPackageEntity> query = cb.createQuery(CRANPackageEntity.class);
    Root<CRANPackageEntity> p = query.from(CRANPackageEntity.class);
    query.where(cb.isFalse(p.get(CRANPackageEntity_.archived)));

    List<CRANPackageEntity> resultList = entityManager.createQuery(query).getResultList();

    for (CRANPackageEntity cRANPackageEntity : resultList) {
        entityManager.remove(cRANPackageEntity);
    }
}

From source file:com.vladmihalcea.HibernateFetchStrategyTest.java

@Test
public void test() {

    final Long productId = transactionTemplate.execute(new TransactionCallback<Long>() {
        @Override//from   w  w  w .  j ava2  s  . c o m
        public Long doInTransaction(TransactionStatus transactionStatus) {

            Company company = new Company();
            company.setName("TV Company");
            entityManager.persist(company);

            Product product = new Product("tvCode");
            product.setName("TV");
            product.setCompany(company);

            Image frontImage = new Image();
            frontImage.setName("front image");
            frontImage.setIndex(0);

            Image sideImage = new Image();
            sideImage.setName("side image");
            sideImage.setIndex(1);

            product.addImage(frontImage);
            product.addImage(sideImage);

            WarehouseProductInfo warehouseProductInfo = new WarehouseProductInfo();
            warehouseProductInfo.setQuantity(101);
            product.addWarehouse(warehouseProductInfo);

            Importer importer = new Importer();
            importer.setName("Importer");
            entityManager.persist(importer);
            product.setImporter(importer);

            entityManager.persist(product);
            return product.getId();
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            LOG.info("Fetch using find");
            Product product = entityManager.find(Product.class, productId);
            assertNotNull(product);
            return null;
        }
    });

    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            LOG.info("Fetch using JPQL");
            Product product = entityManager
                    .createQuery("select p " + "from Product p " + "where p.id = :productId", Product.class)
                    .setParameter("productId", productId).getSingleResult();
            assertNotNull(product);
            return null;
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {

            LOG.info("Fetch using Criteria");

            CriteriaBuilder cb = entityManager.getCriteriaBuilder();
            CriteriaQuery<Product> cq = cb.createQuery(Product.class);
            Root<Product> productRoot = cq.from(Product.class);
            cq.where(cb.equal(productRoot.get("id"), productId));
            Product product = entityManager.createQuery(cq).getSingleResult();
            assertNotNull(product);
            return null;
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            LOG.info("Fetch using join fetch JPQL");

            Product product = product = entityManager.createQuery("select p " + "from Product p "
                    + "inner join fetch p.warehouseProductInfo " + "inner join fetch p.importer", Product.class)
                    .getSingleResult();
            assertNotNull(product);

            return null;
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {
        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {

            Image image = entityManager
                    .createQuery("select i " + "from Image i " + "inner join fetch i.product p "
                            + "where p.id = :productId", Image.class)
                    .setParameter("productId", productId).getResultList().get(0);
            assertNotNull(image);

            return null;
        }
    });
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.SessionTelephonyDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.findAllSessionTelephony = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<SessionTelephonyImpl>>() {
                @Override/*from   w w w. j  a  v a2s .c om*/
                public CriteriaQuery<SessionTelephonyImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<SessionTelephonyImpl> criteriaQuery = cb
                            .createQuery(SessionTelephonyImpl.class);
                    final Root<SessionTelephonyImpl> definitionRoot = criteriaQuery
                            .from(SessionTelephonyImpl.class);
                    criteriaQuery.select(definitionRoot);

                    return criteriaQuery;
                }
            });
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Page<Order> findPage(Member member, Pageable pageable) {
    if (member == null) {
        return new Page<Order>(Collections.<Order>emptyList(), 0, pageable);
    }/* www.j a  v  a  2 s . c  o  m*/
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    return super.findPage(criteriaQuery, pageable);
}

From source file:org.jasig.portlet.blackboardvcportlet.dao.impl.SessionRecordingDaoImpl.java

@Override
public void afterPropertiesSet() throws Exception {
    this.findAllSessionRecordings = this
            .createCriteriaQuery(new Function<CriteriaBuilder, CriteriaQuery<SessionRecordingImpl>>() {
                @Override/*  w  ww. ja  va 2  s. c  o  m*/
                public CriteriaQuery<SessionRecordingImpl> apply(CriteriaBuilder cb) {
                    final CriteriaQuery<SessionRecordingImpl> criteriaQuery = cb
                            .createQuery(SessionRecordingImpl.class);
                    final Root<SessionRecordingImpl> definitionRoot = criteriaQuery
                            .from(SessionRecordingImpl.class);
                    criteriaQuery.select(definitionRoot);

                    return criteriaQuery;
                }
            });
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public List<Order> findList(Member member, Integer count, List<Filter> filters,
        List<net.groupbuy.Order> orders) {
    if (member == null) {
        return Collections.<Order>emptyList();
    }/*w w  w  . ja  v a  2s  . c o m*/
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);
    criteriaQuery.where(criteriaBuilder.equal(root.get("member"), member));
    return super.findList(criteriaQuery, null, count, filters, orders);
}

From source file:net.groupbuy.dao.impl.OrderDaoImpl.java

public Long waitingShippingCount(Member member) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);// www.j  a v a  2  s. c o m
    Predicate restrictions = criteriaBuilder.conjunction();
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.completed),
            criteriaBuilder.notEqual(root.get("orderStatus"), OrderStatus.cancelled),
            criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.paid),
            criteriaBuilder.equal(root.get("shippingStatus"), ShippingStatus.unshipped));
    restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("expire").isNull(),
            criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("expire"), new Date())));
    if (member != null) {
        restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.equal(root.get("member"), member));
    }
    criteriaQuery.where(restrictions);
    return super.count(criteriaQuery, null);
}