Example usage for javax.persistence.criteria CriteriaBuilder createQuery

List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery

Introduction

In this page you can find the example usage for javax.persistence.criteria CriteriaBuilder createQuery.

Prototype

<T> CriteriaQuery<T> createQuery(Class<T> resultClass);

Source Link

Document

Create a CriteriaQuery object with the specified result type.

Usage

From source file:org.jboss.spring3_2.example.MatrixVariables.repo.MemberDaoImpl.java

public List<Member> findByNameAndEmail(String name, String email) {
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Member> criteria = builder.createQuery(Member.class);
    EntityType<Member> type = em.getMetamodel().entity(Member.class);
    Root<Member> member = criteria.from(Member.class);
    if (name != null && !name.isEmpty() && name != "")
        criteria.where(builder.like(member.get(type.getDeclaredSingularAttribute("name", String.class)),
                "%" + name + "%"));
    if (email != null && !email.isEmpty() && email != "")
        criteria.where(builder.like(member.get(type.getDeclaredSingularAttribute("email", String.class)),
                "%" + email + "%"));
    return em.createQuery(criteria).getResultList();
}

From source file:core.commonapp.server.dao.security.PermissionSecurityGroupDaoHibernateImpl.java

@Override
public List<PermissionSecurityGroup> findAllBySecurityGroup(SecurityGroup securityGroup, boolean includeOld) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<PermissionSecurityGroup> query = builder.createQuery(getPersistClass());
    Root<PermissionSecurityGroup> root = query.from(getPersistClass());
    query.where(builder.equal(root.get("securityGroup"), securityGroup));

    if (!includeOld) {
        query.where(builder.isNull(root.get("thruDate")));
    }// w  w  w  .j  av  a 2 s  .c  om

    return (List<PermissionSecurityGroup>) getEntityManager().createQuery(query).getResultList();
}

From source file:de.logicline.splash.dao.ContactDaoImpl.java

public List<ContactEntity> findByName(String name) {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<ContactEntity> cq = cb.createQuery(ContactEntity.class);
    Root<ContactEntity> userInfo = cq.from(ContactEntity.class);
    cq.select(userInfo);/*  ww  w.j  a v  a  2s  . c  o  m*/
    cq.where(cb.like(userInfo.<String>get("lastName"), "%" + name + "%"));
    List<ContactEntity> result = getEntityManager().createQuery(cq).getResultList();
    return result;
}

From source file:com.nuevebit.persistence.repository.JPASearchableRepository.java

@Override
public final List<T> search(S searchCriteria, Pageable pageable) {
    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<T> query = cb.createQuery(getEntityClass());

    Root<T> p = createQueryRoot(query, searchCriteria);
    query.select(p);//from   w  w  w .j  a  va 2  s . c  o m

    Order sortOrder = getSortOrder(cb);
    if (sortOrder != null) {
        query.orderBy(sortOrder);
    }

    TypedQuery<T> typedQuery = getEntityManager().createQuery(query);
    if (pageable != null) {
        typedQuery.setFirstResult(pageable.getOffset()).setMaxResults(pageable.getPageSize());
    }

    return typedQuery.getResultList();
}

From source file:cz.muni.fi.pa165.bookingmanager.managers.UserManager.java

@Override
public List<User> findAll() {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

    CriteriaQuery<User> query = criteriaBuilder.createQuery(User.class);
    Root<User> c = query.from(User.class);
    query.select(c);//from   w  w  w  . j  a  v  a 2s  .  com

    TypedQuery<User> typedQuery = entityManager.createQuery(query);

    return typedQuery.getResultList();
    //return em.createQuery("SELECT u FROM User u WHERE u.user = :user", User.class).setParameter("user", user).getResultList();
}

From source file:dtu.ds.warnme.dao.impl.EventsDaoImpl.java

@Override
public List<EventEntity> getNearestEvents(Float nLat, Float sLat, Float eLng, Float wLng,
        EventType... eventTypes) {/*from www  .  j  a v a 2 s  .co  m*/
    CriteriaBuilder criteriaBuilder = getCriteriaBuilder();
    CriteriaQuery<EventEntity> criteriaQuery = criteriaBuilder.createQuery(EventEntity.class);
    Root<EventEntity> root = criteriaQuery.from(EventEntity.class);

    List<Predicate> predicates = new ArrayList<Predicate>();
    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.latitude), nLat));
    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.latitude), sLat));
    predicates.add(criteriaBuilder.lessThanOrEqualTo(root.get(EventEntity_.longitude), eLng));
    predicates.add(criteriaBuilder.greaterThanOrEqualTo(root.get(EventEntity_.longitude), wLng));

    if (ArrayUtils.isNotEmpty(eventTypes)) {
        List<Predicate> eventTypePredicates = new ArrayList<Predicate>();
        for (EventType et : eventTypes) {
            eventTypePredicates.add(criteriaBuilder.equal(root.get(EventEntity_.eventType), et));
        }
        predicates.add(criteriaBuilder.and(
                criteriaBuilder.or(eventTypePredicates.toArray(new Predicate[eventTypePredicates.size()]))));
    }

    criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()]));
    return getAllByCriteria(criteriaQuery);
}

From source file:com.ttech.cordovabuild.domain.application.ApplicationRepositoryImpl.java

@Override
public List<ApplicationBuilt> getApplications(User owner) {
    CriteriaBuilder cb = em.getCriteriaBuilder();
    CriteriaQuery<ApplicationBuilt> cq = cb.createQuery(ApplicationBuilt.class);
    Root<ApplicationBuilt> from = cq.from(ApplicationBuilt.class);
    return em//from  www . j av  a 2  s  . com
            .createQuery(cq.select(from)
                    .where(cb.equal(from.get(ApplicationBuilt_.application).get(Application_.owner), owner)))
            .getResultList();
}

From source file:de.logicline.splash.dao.ContactDaoImpl.java

public ContactEntity getContactByUserId(String userId) {

    CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<ContactEntity> cq = cb.createQuery(ContactEntity.class);
    Root<ContactEntity> userInfo = cq.from(ContactEntity.class);
    cq.select(userInfo);//from www  . java 2 s.c  o  m
    cq.where(cb.equal(userInfo.get("userIdFk"), userId));
    List<ContactEntity> resultList = em.createQuery(cq).getResultList();

    ContactEntity result = null;
    if (resultList != null && resultList.size() > 0) {
        result = resultList.get(0);
    }

    return result;
}

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

public Page<Coupon> findPage(Boolean isEnabled, Boolean isExchange, Boolean hasExpired, Pageable pageable) {
    CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
    CriteriaQuery<Coupon> criteriaQuery = criteriaBuilder.createQuery(Coupon.class);
    Root<Coupon> root = criteriaQuery.from(Coupon.class);
    criteriaQuery.select(root);/*from   w w w . j av  a2  s .c o  m*/
    Predicate restrictions = criteriaBuilder.conjunction();
    if (isEnabled != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isEnabled"), isEnabled));
    }
    if (isExchange != null) {
        restrictions = criteriaBuilder.and(restrictions,
                criteriaBuilder.equal(root.get("isExchange"), isExchange));
    }
    if (hasExpired != null) {
        if (hasExpired) {
            restrictions = criteriaBuilder.and(restrictions, root.get("endDate").isNotNull(),
                    criteriaBuilder.lessThan(root.<Date>get("endDate"), new Date()));
        } else {
            restrictions = criteriaBuilder.and(restrictions, criteriaBuilder.or(root.get("endDate").isNull(),
                    criteriaBuilder.greaterThanOrEqualTo(root.<Date>get("endDate"), new Date())));
        }
    }
    criteriaQuery.where(restrictions);
    return super.findPage(criteriaQuery, pageable);
}

From source file:core.commonapp.server.dao.security.UserLoginDaoHibernateImpl.java

@Override
public boolean existByUsername(String username) {
    CriteriaBuilder builder = getEntityManager().getCriteriaBuilder();
    CriteriaQuery<UserLogin> query = builder.createQuery(UserLogin.class);

    Root<UserLogin> root = query.from(UserLogin.class);

    query.where(builder.equal(root.get("username"), username));

    return getEntityManager().createQuery(query).getResultList().size() > 0;
}