Example usage for javax.persistence.criteria Root get

List of usage examples for javax.persistence.criteria Root get

Introduction

In this page you can find the example usage for javax.persistence.criteria Root get.

Prototype

<Y> Path<Y> get(SingularAttribute<? super X, Y> attribute);

Source Link

Document

Create a path corresponding to the referenced single-valued attribute.

Usage

From source file:com.home.ln_spring.ch10.service.jpa.ContactServiceImplOld.java

@Override
@Transactional(readOnly = true)//from w w  w  . j a  va 2  s.com
public List<Contact> findByCriteriaQuery(String firstName, String lastName) {
    log.info("Finding contact for firstName: " + firstName + " and lastName: " + lastName);

    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Contact> criteriaQuery = cb.createQuery(Contact.class);
    Root<Contact> contactRoot = criteriaQuery.from(Contact.class);
    contactRoot.fetch(Contact_.contactTelDetails, JoinType.LEFT);
    contactRoot.fetch(Contact_.hobbies, JoinType.LEFT);

    criteriaQuery.select(contactRoot).distinct(true);

    Predicate criteria = cb.conjunction();
    if (firstName != null) {
        Predicate p = cb.equal(contactRoot.get(Contact_.firstName), firstName);
        criteria = cb.and(criteria, p);
    }

    if (lastName != null) {
        Predicate p = cb.equal(contactRoot.get(Contact_.lastName), lastName);
        criteria = cb.and(criteria, p);
    }

    criteriaQuery.where(criteria);
    List<Contact> result = em.createQuery(criteriaQuery).getResultList();
    return result;
}

From source file:com.dbs.sdwt.jpa.ByExampleUtil.java

protected <T extends Identifiable<?>> List<Predicate> byExampleOnCompositePk(Root<T> root, T entity,
        SearchParameters sp, CriteriaBuilder builder) {
    String compositePropertyName = jpaUtil.compositePkPropertyName(entity);
    if (compositePropertyName == null) {
        return emptyList();
    } else {//from   w w w. j a v  a  2s  .  c o m
        return newArrayList(
                byExampleOnEmbeddable(root.get(compositePropertyName), entity.getId(), sp, builder));
    }
}

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);/*from w  w  w.j a v a 2  s.  co  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);
}

From source file:org.jboss.quickstarts.wfk.contact.ContactRepository.java

/**
 * Find just one Contact by the last name that is passed in. If there is more then one, only the first will be returned.
 * /*from  ww  w .  j ava2s .  c o  m*/
 * @param lastName
 * @return Contact
 */
Contact findByLastName(String lastName) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Contact> criteria = cb.createQuery(Contact.class);
    Root<Contact> contact = criteria.from(Contact.class);
    // Swap criteria statements if you would like to try out type-safe criteria queries, a new feature in JPA 2.0.
    // criteria.select(contact).where(cb.equal(contact.get(Contact_.lastName), lastName));
    criteria.select(contact).where(cb.equal(contact.get("lastName"), lastName));
    return em.createQuery(criteria).getSingleResult();
}

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

public Long waitingPaymentCount(Member member) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Order> criteriaQuery = criteriaBuilder.createQuery(Order.class);
    Root<Order> root = criteriaQuery.from(Order.class);
    criteriaQuery.select(root);//from  ww w  .jav  a  2s .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));
    restrictions = criteriaBuilder.and(restrictions,
            criteriaBuilder.or(criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.unpaid),
                    criteriaBuilder.equal(root.get("paymentStatus"), PaymentStatus.partialPayment)));
    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);
}

From source file:org.jboss.quickstarts.wfk.contact.ContactRepository.java

/**
 * Find just one Contact by the first name that is passed in. If there is more then one, only the first will be returned.
 * // w w  w  .j a  va  2s. com
 * @param firstName
 * @return Contact
 */
Contact findByFirstName(String firstName) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<Contact> criteria = cb.createQuery(Contact.class);
    Root<Contact> contact = criteria.from(Contact.class);
    // Swap criteria statements if you would like to try out type-safe criteria queries, a new feature in JPA 2.0.
    // criteria.select(contact).where(cb.equal(contact.get(Contact_.firstName), firstName));
    criteria.select(contact).where(cb.equal(contact.get("firstName"), firstName));
    return em.createQuery(criteria).getSingleResult();
}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> forOrganisation(final Organisation organisation)
        throws ArgumentNullOrEmptyException {
    if (organisation == null) {
        throw new ArgumentNullOrEmptyException("organisation");
    }/* www .  j av a2  s  .co  m*/

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {

            final Subquery<Long> subquery = query.subquery(Long.class);
            final Root<DeviceAuthorization> deviceAuthorizationRoot = subquery.from(DeviceAuthorization.class);
            subquery.select(deviceAuthorizationRoot.get("device").get("id").as(Long.class));
            subquery.where(cb.equal(deviceAuthorizationRoot.get("organisation"), organisation.getId()));

            return cb.in(deviceRoot.get("id")).value(subquery);
        }
    };
}

From source file:com.alliander.osgp.adapter.ws.infra.specifications.JpaDeviceSpecifications.java

@Override
public Specification<Device> forOwner(final String organisation) throws ArgumentNullOrEmptyException {
    if (organisation == null) {
        throw new ArgumentNullOrEmptyException("owner");
    }/*from   ww  w  . j  av a 2  s. com*/

    return new Specification<Device>() {
        @Override
        public Predicate toPredicate(final Root<Device> deviceRoot, final CriteriaQuery<?> query,
                final CriteriaBuilder cb) {

            final Subquery<Long> subquery = query.subquery(Long.class);
            final Root<DeviceAuthorization> deviceAuthorizationRoot = subquery.from(DeviceAuthorization.class);
            subquery.select(deviceAuthorizationRoot.get("device").get("id").as(Long.class));
            subquery.where(cb.and(
                    cb.like(cb.upper(deviceAuthorizationRoot.get("organisation").<String>get("name")),
                            organisation.toUpperCase()),
                    cb.equal(deviceAuthorizationRoot.get("functionGroup"),
                            DeviceFunctionGroup.OWNER.ordinal())));
            return cb.in(deviceRoot.get("id")).value(subquery);
        }
    };
}

From source file:eu.uqasar.service.AbstractService.java

/**
 * Method to get the entity of class by a given name.
 * @param clazz Class/*from w  w  w. j  a  v a 2  s.c o  m*/
 * @param name attribute name of the searched entity
 * @return T entity
 */
public <T extends Persistable> T getByName(Class<T> clazz, String name) {
    logger.infof("loading %s with name %d ...", getReadableClassName(clazz), name);
    T entity = null;

    CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
    CriteriaQuery<T> criteriaQuery = criteriaBuilder.createQuery(clazz);
    Root<T> ent = criteriaQuery.from(clazz);
    criteriaQuery.select(ent).where(criteriaBuilder.equal(ent.get("name"), name));

    List<T> entities = em.createQuery(criteriaQuery).getResultList();

    if (entities != null && entities.size() > 0) {
        entity = entities.get(0);
    }
    return entity;
}