List of usage examples for javax.persistence.criteria CriteriaBuilder createQuery
<T> CriteriaQuery<T> createQuery(Class<T> resultClass);
CriteriaQuery
object with the specified result type. From source file:ch.puzzle.itc.mobiliar.business.server.boundary.ServerView.java
public List<ServerTuple> getNodeServers(String hostFilter, String appServerFilter, String runtimeFilter, String nodeFilter, String contextFilter) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ServerTuple> q = cb.createQuery(ServerTuple.class); //get Node ResType Root<ResourceEntity> node = q.from(ResourceEntity.class); Join<ResourceEntity, ResourceTypeEntity> nodeType = node.join("resourceType", JoinType.LEFT); Join<ResourceEntity, ReleaseEntity> nodeRelease = node.join("release", JoinType.LEFT); //get Props on node Join<ResourceEntity, ResourceContextEntity> resCtx = node.join("contexts", JoinType.LEFT); Join<ResourceContextEntity, ContextEntity> nodeCtx = resCtx.join("context", JoinType.LEFT); Join<ContextEntity, ContextEntity> nodeDomain = nodeCtx.join("parent", JoinType.LEFT); Join<ResourceContextEntity, PropertyEntity> nodeProp = resCtx.join("properties", JoinType.LEFT); Join<PropertyEntity, PropertyDescriptorEntity> nodePropType = nodeProp.join("descriptor", JoinType.LEFT); //get AppServer Join<ResourceEntity, ConsumedResourceRelationEntity> nodeResRel = node.join("consumedSlaveRelations", JoinType.LEFT);/*from w w w .j ava 2 s . c o m*/ Join<ConsumedResourceRelationEntity, ResourceEntity> appServer = nodeResRel.join("masterResource", JoinType.LEFT); Join<ResourceEntity, ReleaseEntity> asRelease = appServer.join("release", JoinType.LEFT); Join<ResourceEntity, ResourceTypeEntity> asType = appServer.join("resourceType", JoinType.LEFT); //get Runtime of as Join<ResourceEntity, ConsumedResourceRelationEntity> asResRel = appServer.join("consumedMasterRelations", JoinType.LEFT); Join<ConsumedResourceRelationEntity, ResourceEntity> asRuntime = asResRel.join("slaveResource", JoinType.LEFT); Join<ResourceEntity, ResourceTypeEntity> runtimeType = asRuntime.join("resourceType", JoinType.LEFT); q.select(cb.construct(ServerTuple.class, nodeProp.get("value"), appServer.get("name"), appServer.get("id"), asRelease.get("name"), asRuntime.get("name"), node.get("name"), node.get("id"), nodeRelease.get("name"), nodeDomain.get("name"), nodeDomain.get("id"), nodeCtx.get("name"), nodeCtx.get("id"), cb.literal(1) // true )); Predicate p = cb.and(cb.equal(nodeType.get("name"), DefaultResourceTypeDefinition.NODE.name()), cb.or(cb.equal(asType.get("name"), DefaultResourceTypeDefinition.APPLICATIONSERVER.name()), cb.isNull(asType.get("name")) //nodes without appServer ), cb.or(cb.equal(runtimeType.get("name"), ResourceTypeEntity.RUNTIME), cb.isNull(runtimeType.get("name"))), cb.isNotNull(nodeProp.get("value")), cb.equal(nodePropType.get("propertyName"), "hostName") ); p = addFilters(p, cb, hostFilter, appServerFilter, runtimeFilter, nodeFilter, contextFilter, nodeProp.<String>get("value"), appServer.<String>get("name"), asRuntime.<String>get("name"), node.<String>get("name"), nodeCtx.<String>get("name")); q.where(p); TypedQuery<ServerTuple> query = entityManager.createQuery(q); List<ServerTuple> servers = query.getResultList(); return servers; }
From source file:ch.puzzle.itc.mobiliar.business.server.boundary.ServerView.java
public List<ServerTuple> getAppServers(String hostFilter, String appServerFilter, String runtimeFilter, String nodeFilter, String contextFilter) { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<ServerTuple> q = cb.createQuery(ServerTuple.class); //get Node ResType Root<ResourceEntity> node = q.from(ResourceEntity.class); Join<ResourceEntity, ResourceTypeEntity> nodeType = node.join("resourceType", JoinType.LEFT); Join<ResourceEntity, ReleaseEntity> nodeRelease = node.join("release", JoinType.LEFT); //get AppServer Join<ResourceEntity, ConsumedResourceRelationEntity> nodeResRel = node.join("consumedSlaveRelations", JoinType.LEFT);// w ww . j a v a 2s . c o m Join<ConsumedResourceRelationEntity, ResourceEntity> appServer = nodeResRel.join("masterResource", JoinType.LEFT); Join<ResourceEntity, ReleaseEntity> asRelease = appServer.join("release", JoinType.LEFT); Join<ResourceEntity, ResourceTypeEntity> asType = appServer.join("resourceType", JoinType.LEFT); //get Runtime of as Join<ResourceEntity, ConsumedResourceRelationEntity> asResRel = appServer.join("consumedMasterRelations", JoinType.LEFT); Join<ConsumedResourceRelationEntity, ResourceEntity> asRuntime = asResRel.join("slaveResource", JoinType.LEFT); Join<ResourceEntity, ResourceTypeEntity> runtimeType = asRuntime.join("resourceType", JoinType.LEFT); //get Props between as and node Join<ConsumedResourceRelationEntity, ResourceRelationContextEntity> resRelCtx = nodeResRel.join("contexts", JoinType.LEFT); Join<ResourceRelationContextEntity, ContextEntity> asCtx = resRelCtx.join("context", JoinType.LEFT); Join<ContextEntity, ContextEntity> asDomain = asCtx.join("parent", JoinType.LEFT); Join<ResourceRelationContextEntity, PropertyEntity> asProp = resRelCtx.join("properties", JoinType.LEFT); //here an on clause should be added, so we don't get hostnames that are divined directly on the node multiple times (and descriptor.propertyName = 'hostName') //on support was added in jpa 2.1 which is part of JEE 7 Join<PropertyEntity, PropertyDescriptorEntity> asPropType = asProp.join("descriptor", JoinType.LEFT); q.select(cb.construct(ServerTuple.class, asProp.get("value"), appServer.get("name"), appServer.get("id"), asRelease.get("name"), asRuntime.get("name"), node.get("name"), node.get("id"), nodeRelease.get("name"), asDomain.get("name"), asDomain.get("id"), asCtx.get("name"), asCtx.get("id"), cb.literal(0) //false )); Predicate p = cb.and(cb.equal(nodeType.get("name"), DefaultResourceTypeDefinition.NODE.name()), cb.equal(asType.get("name"), DefaultResourceTypeDefinition.APPLICATIONSERVER.name()), cb.or(cb.equal(runtimeType.get("name"), ResourceTypeEntity.RUNTIME), cb.isNull(runtimeType.get("name"))), cb.isNotNull(asProp.get("value")), cb.equal(asPropType.get("propertyName"), "hostName")); p = addFilters(p, cb, hostFilter, appServerFilter, runtimeFilter, nodeFilter, contextFilter, asProp.<String>get("value"), appServer.<String>get("name"), asRuntime.<String>get("name"), node.<String>get("name"), asCtx.<String>get("name")); q.where(p); TypedQuery<ServerTuple> query = entityManager.createQuery(q); List<ServerTuple> servers = query.getResultList(); return servers; }
From source file:com.aimdek.ccm.dao.impl.test.UsersDaoImplTest.java
/** * Test user like./*from w w w . j ava2 s. co m*/ */ @Test public void testUserLike() { CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> root = query.from(User.class); query.where(builder.like(root.<String>get("lastName"), "test%")); List<User> user = entityManager.createQuery(query).getResultList(); assertEquals(10, user.size()); }
From source file:org.businessmanager.dao.GenericDaoImpl.java
@Override public Long getCount(Map<SingularAttribute<T, ?>, Object> filterAttributes, boolean enableLikeSearch) { CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<Long> criteriaQuery = queryBuilder.createQuery(Long.class); Root<T> rootQuery = criteriaQuery.from(getPersistenceClass()); CriteriaQuery<Long> select = criteriaQuery.select(queryBuilder.count(rootQuery)); List<Predicate> predicateList = createFilterList(filterAttributes, enableLikeSearch, queryBuilder, rootQuery);/*w w w. j a va2 s. c o m*/ criteriaQuery.where(predicateList.toArray(new Predicate[0])); TypedQuery<Long> typedQuery = entityManager.createQuery(select); return typedQuery.getSingleResult(); }
From source file:name.marcelomorales.siqisiqi.openjpa.impl.OrmFinderImpl.java
@Override @TransactionAttribute/*from w ww . jav a 2s . c o m*/ public List<T> findByFullTexts(final String terms, final OrderBy<T> orders, final long first, final long count, final String... fg) { LOGGER.debug("searching by text {} terms {}, fg {}", new Object[] { persistentClass, terms, fg }); if (settings.returnsNullIfTermsAreNull() && Strings.isNullOrEmpty(terms)) { return Collections.emptyList(); } if (fg != null) { entityManager.pushFetchPlan(); entityManager.getFetchPlan().addFetchGroups(fg); } CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<T> q = cb.createQuery(persistentClass); Root<T> p = q.from(persistentClass); q.where(newFullTextPredicate(cb, p, terms)); if (orders != null) { List<Order> orderList = orders.orders(cb, p); if (orderList != null && !orderList.isEmpty()) { q.orderBy(orderList); } } TypedQuery<T> query = entityManager.createQuery(q); OrmUtils.firstCount(query, first, count); List<T> resultList = query.getResultList(); if (fg != null) { entityManager.popFetchPlan(); } return resultList; }
From source file:com.carser.viamais.vo.TransactionFilter.java
public List<Transaction> resultList(final EntityManager em) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Transaction> cq = cb.createQuery(Transaction.class); Root<Transaction> transaction = cq.from(Transaction.class); cq.where(getPredicates(cb, transaction)); @SuppressWarnings("rawtypes") Path path;//from w ww . j a v a 2 s .c om switch (orderBy) { case CUSTOMER_NAME: path = transaction.get(Transaction_.car).get(Car_.model).get(Model_.name); break; case DATE: path = transaction.get(Transaction_.transactionDate); break; case PLATE: path = transaction.get(Transaction_.car).get(Car_.licensePlate); break; case TYPE: path = transaction.get(Transaction_.type); break; case YEAR: path = transaction.get(Transaction_.car).get(Car_.yearOfModel); break; default: path = transaction.get(Transaction_.car).get(Car_.model).get(Model_.name); break; } switch (order) { case DESC: cq.orderBy(cb.desc(path)); break; default: cq.orderBy(cb.asc(path)); break; } TypedQuery<Transaction> query = em.createQuery(cq); query.setFirstResult(offset); query.setMaxResults(limit); return query.getResultList(); }
From source file:org.openmeetings.app.data.user.dao.UsersDaoImpl.java
public List<Users> getAllUsers() { try {/*ww w .j ava 2 s . co m*/ // get all non-deleted users CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Users> cq = cb.createQuery(Users.class); Root<Users> c = cq.from(Users.class); Predicate condition = cb.equal(c.get("deleted"), "false"); cq.where(condition); TypedQuery<Users> q = em.createQuery(cq); List<Users> ll = q.getResultList(); return ll; } catch (Exception ex2) { log.error("[getAllUsers] ", ex2); } return null; }
From source file:com.aimdek.ccm.dao.impl.test.UsersDaoImplTest.java
/** * Test retrieve limited user.//from w w w . j av a 2 s . co m */ @Test public void testRetrieveLimitedUser() { int limit = 10; int start = 5; CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery<User> query = builder.createQuery(User.class); Root<User> root = query.from(User.class); query.where(builder.equal(root.get(FIELDCONSTANT_USER_ROLE), ROLE_CUSTOMER)); List<User> user = entityManager.createQuery(query).setFirstResult(start).setMaxResults(limit) .getResultList(); assertEquals(user.size(), 5); }
From source file:org.businessmanager.dao.GenericDaoImpl.java
@Override public List<T> findByAttributes(Map<SingularAttribute<T, ?>, Object> theAttributes) { CriteriaBuilder queryBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass()); Root<T> rootQuery = criteriaQuery.from(getPersistenceClass()); CriteriaQuery<T> select = criteriaQuery.select(rootQuery); List<Predicate> aPredicateList = new ArrayList<Predicate>(); Iterator<SingularAttribute<T, ?>> anIterator = theAttributes.keySet().iterator(); while (anIterator.hasNext()) { SingularAttribute<T, ?> aKey = anIterator.next(); Object aValue = theAttributes.get(aKey); Predicate aPredicate = queryBuilder.equal(rootQuery.get(aKey), aValue); aPredicateList.add(aPredicate);//from www .j a va2s . c o m } select.where(aPredicateList.toArray(new Predicate[0])); TypedQuery<T> typedQuery = entityManager.createQuery(select); return typedQuery.getResultList(); }
From source file:org.businessmanager.dao.GenericDaoImpl.java
public List<T> findByAssignedEntity(ListAttribute<T, ?> listAttribute, Long entityId) { CriteriaBuilder queryBuilder = getEntityManager().getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = queryBuilder.createQuery(getPersistenceClass()); Root<T> rootQuery = criteriaQuery.from(getPersistenceClass()); CriteriaQuery<T> select = criteriaQuery.select(rootQuery); Join<T, ?> memberJoin = rootQuery.join(listAttribute); Path<?> nameField = memberJoin.get("id"); Predicate nameEquals = queryBuilder.equal(nameField, entityId); criteriaQuery.where(nameEquals);/*from w w w .j ava2 s .c om*/ TypedQuery<T> typedQuery = getEntityManager().createQuery(select); return typedQuery.getResultList(); }