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:com.eu.evaluation.server.dao.AbstractDAO.java
public boolean isUnique(T entity, String... propertys) { if (propertys == null || propertys.length == 0) { return true; }/*from ww w. j av a 2s . co m*/ try { CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery<T> criteriaQuery = cb.createQuery(entityClass); Root<T> root = criteriaQuery.from(entityClass); Predicate predicate = null; for (String property : propertys) { if (predicate == null) { predicate = cb.equal(root.get(property), PropertyUtils.getProperty(entity, property)); } else { predicate = cb.and(predicate, cb.equal(root.get(property), PropertyUtils.getProperty(entity, property))); } } if (!StringUtils.isBlank(entity.getId())) { predicate = cb.and(predicate, cb.notEqual(root.get("id"), entity.getId())); } criteriaQuery.where(predicate); TypedQuery<T> typedQuery = entityManager.createQuery(criteriaQuery); List<T> result = typedQuery.getResultList(); return result.isEmpty(); } catch (Exception e) { e.printStackTrace(); ReflectionUtils.handleReflectionException(e); } return false; }
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. *//* w w w .java 2 s . com*/ 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:net.echinopsii.ariane.community.plugin.rabbitmq.directory.RabbitmqDirectoryBootstrap.java
private void plugDirectoryJPAProvider() { Company pivotal = null;/*w ww. j a va 2 s . c o 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:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * use aliases/*from w ww . j a v a 2s . co 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' *///ww w.j a v a2s . c om @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:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * order by:// ww w . j a v a 2s .c o m * 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); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * SELECT e FROM jpa_query_employee// ww w . j a v a 2 s . co m */ @Transactional public void doSimple() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); c.select(e); showResult(c); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * SELECT e FROM jpa_query_employee e WHERE e.name='employee_1' *///ww w . jav a2 s . co m @Transactional public void doSimple2() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); c.select(e).where(cb.equal(e.get("name"), "employee_1")); showResult(c); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * group by and having :/* w ww. j a v a2 s .c o m*/ * SELECT e, COUNT(p) FROM jpa_query_employee e JOIN e.projects p GROUP BY e HAVING COUNT(p) >= 2 */ @Transactional public void doGroupby() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Object[]> c = cb.createQuery(Object[].class); Root<Employee> e = c.from(Employee.class); Join<Employee, Project> p = e.join("projects"); c.multiselect(e, cb.count(p)).groupBy(e).having(cb.ge(cb.count(p), 2)); showResult(c); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * out join/* w w w . jav a 2s . co m*/ */ @Transactional public void doFrom2() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Object[]> c = cb.createQuery(Object[].class); Root<Employee> e = c.from(Employee.class); Join<Object, Object> address = e.join("address", JoinType.LEFT); Join<Object, Object> department = e.join("department", JoinType.LEFT); c.multiselect(e, address, department); showResult(c); }