List of usage examples for javax.persistence.criteria CriteriaQuery from
<X> Root<X> from(Class<X> entityClass);
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);//from ww w. ja v a 2 s . co 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.creditcloud.common.entities.dao.AbstractReadDAO.java
/** * count all entity/*from w w w . j a v a 2 s . co m*/ * * @return */ public int count() { EntityManager em = getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(); Root<T> rt = cq.from(entityClass); cq.select(cb.count(rt)); Query q = em.createQuery(cq); Object result = q.getSingleResult(); return result == null ? 0 : ((Long) result).intValue(); }
From source file:net.echinopsii.ariane.community.plugin.rabbitmq.directory.RabbitmqDirectoryBootstrap.java
private void plugDirectoryJPAProvider() { Company pivotal = null;/*from w w w . j ava 2 s .co m*/ Application rabbitmq = null; directoryJpaProvider.addSubPersistenceBundle(FrameworkUtil.getBundle(RabbitmqDirectoryBootstrap.class)); EntityManager em = directoryJpaProvider.createEM(); CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery<Company> cmpCriteria = builder.createQuery(Company.class); Root<Company> cmpRoot = cmpCriteria.from(Company.class); cmpCriteria.select(cmpRoot).where(builder.equal(cmpRoot.<String>get("name"), "Pivotal")); TypedQuery<Company> cmpQuery = em.createQuery(cmpCriteria); try { pivotal = cmpQuery.getSingleResult(); log.debug("Pivotal company already defined ..."); } catch (NoResultException e) { log.debug("Pivotal company will be defined ..."); } catch (Exception e) { throw e; } CriteriaQuery<Application> appCriteria = builder.createQuery(Application.class); Root<Application> appRoot = appCriteria.from(Application.class); appCriteria.select(appRoot).where(builder.equal(appRoot.<String>get("name"), "RabbitMQ")); TypedQuery<Application> appQuery = em.createQuery(appCriteria); try { rabbitmq = appQuery.getSingleResult(); log.debug("RabbitMQ application already defined ..."); } catch (NoResultException e) { log.debug("RabbitMQ application will be defined ..."); } catch (Exception e) { throw e; } em.getTransaction().begin(); if (pivotal == null) { pivotal = new Company().setNameR("Pivotal").setDescriptionR("Pivotal"); em.persist(pivotal); } if (rabbitmq == null) { rabbitmq = new Application().setNameR("RabbitMQ").setCompanyR(pivotal).setShortNameR("RabbitMQ") .setColorCodeR("ff6600").setDescriptionR("Robust messaging for applications"); em.persist(rabbitmq); } if (!pivotal.getApplications().contains(rabbitmq)) { pivotal.getApplications().add(rabbitmq); } em.flush(); em.getTransaction().commit(); }
From source file:edu.sabanciuniv.sentilab.sare.models.base.documentStore.PersistentDocumentStore.java
/** * Gets identifiers of documents in this store. * @param em the {@link EntityManager} to get the documents from. * @return an {@link Iterable} of document identifiers. *//* www .j a v a2s. c o m*/ public Iterable<byte[]> getDocumentIds(EntityManager em) { Validate.notNull(em, CannedMessages.NULL_ARGUMENT, "em"); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<byte[]> cq = cb.createQuery(byte[].class); Root<PersistentDocument> doc = cq.from(PersistentDocument.class); cq.multiselect(doc.get("id")) .where(cb.equal(doc.get("store"), cb.parameter(PersistentDocumentStore.class, "store"))); TypedQuery<byte[]> tq = em.createQuery(cq); tq.setParameter("store", this); return tq.getResultList(); }
From source file:eu.uqasar.service.AbstractService.java
private <T extends Persistable> List<T> getRange(Class<T> clazz, int first, int count) { logger.infof("loading %d %s starting from %d ...", count, getReadableClassName(clazz), first); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> query = cb.createQuery(clazz); query.from(clazz); return em.createQuery(query).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * use aliases//from ww w. ja v a 2 s . c o m */ @Transactional public void doSelect4() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<String> c = cb.createQuery(String.class); Root<Employee> e = c.from(Employee.class); c.select(e.<String>get("name").alias("fullname")); showResult(c); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * SELECT e.name FROM jpa_query_employee' */// w ww . ja v a 2 s . c o m @Transactional public void doSelect1() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<String> c = cb.createQuery(String.class); Root<Employee> e = c.from(Employee.class); c.select(e.<String>get("name")); showResult(c); }
From source file:eu.uqasar.service.AbstractService.java
public <T extends Persistable> List<T> getAll(Class<T> clazz) { logger.infof("loading all %ss ...", getReadableClassName(clazz)); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> criteria = cb.createQuery(clazz); criteria.from(clazz); return em.createQuery(criteria).getResultList(); }
From source file:eu.uqasar.service.AbstractService.java
private <T extends Persistable> List<T> getRangeOrdered(Class<T> clazz, int first, int count, Order... orders) { logger.infof("loading %d %s starting from %d ...", count, getReadableClassName(clazz), first); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> query = cb.createQuery(clazz); query.from(clazz); if (orders != null && orders.length > 0) { query.orderBy(orders);//from w w w . j a v a 2s . com } return em.createQuery(query).setFirstResult(first).setMaxResults(count).getResultList(); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * order by://from w ww. j av a 2 s . com * SELECT e.name, e.salary FROM jpa_query_employee e ORDER BY e.name DESC, e.salary DESC */ public void doOrderby() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Object[]> c = cb.createQuery(Object[].class); Root<Employee> e = c.from(Employee.class); c.multiselect(e.get("name"), e.get("salary")).orderBy(cb.desc(e.get("name")), cb.desc(e.get("salary"))); showResult(c); }