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:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * in and path:/*from www.j a v a 2 s. c o m*/ * SELECT e FROM jpa_query_employee e WHERE e.address.state IN ('NY','MI') */ @Transactional public void doWhere3() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); // method 1 c.select(e).where(e.get("address").get("state").in("NY", "MI")); showResult(c); // method 2 c.select(e).where(cb.in(e.get("address").get("state")).value("NY").value("MI")); showResult(c); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * inner join/*from w w w. j a v a 2 s.co m*/ */ @Transactional public void doFrom1() { 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"); Join<Object, Object> department = e.join("department"); c.multiselect(e, address, department); // .where(cb.and( // cb.equal(address.get("state"), "NY")), // cb.greaterThan(e.<Long>get("salary"), Long.valueOf(1500)), // cb.like(department.<String>get("name"), "department%" // )); showResult(c); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * subquery: in//w w w .j ava2 s . c o m * SELECT e FROM jpa_query_employee e WHERE e IN (SELECT emp FROM jpa_query_project p JOIN p.employees pe WHERE p.name = :projectname) */ @Transactional public void doWhere1() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); // subquery Subquery<Employee> sq = c.subquery(Employee.class); Root<Project> p = sq.from(Project.class); Join<Project, Employee> pe = p.join("employees"); sq.select(pe).where(cb.equal(p.get("name"), cb.parameter(String.class, "projectname"))); // c.select(e).where(cb.in(e).value(sq)); }
From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java
public <K> T findBy(String fieldName, Object value, Class<K> valueClass) { EntityManager em = getEntityManager(); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> cq = cb.createQuery(entityClass); Root<T> root = cq.from(entityClass); cq.select(root).where(cb.equal(root.get(fieldName).as(valueClass), value)); List<T> list = em.createQuery(cq).getResultList(); return list.isEmpty() ? null : list.get(0); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * subquery(equivalent to dowhere1) : exists * SELECT e FROM jpa_query_employee e WHERE EXISTS (SELECT p FROM e.projects p WHERE p.name = :projectname) */// w ww. j ava 2 s. c o m @Transactional public void doWhere2() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); // subquery Subquery<Project> sq = c.subquery(Project.class); // Root<Project> p = sq.from(Project.class); Root<Employee> se = sq.correlate(e); Join<Employee, Project> p = se.join("projects"); sq.select(p).where(cb.equal(p.get("name"), cb.parameter(String.class, "projectname"))); // c.select(e).where(cb.exists(sq)); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * SELECT e FROM jpa_query_employee e WHERE e.name= :name *//*from ww w . j a va 2 s . c o m*/ @Transactional public void doParameterQuery() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); // parameter, equal to ":name" ParameterExpression<String> paraName = cb.parameter(String.class, "name"); // e.name = ":name" c.select(e).where(cb.equal(e.get("name"), paraName)); // set param value TypedQuery<Employee> query = em.createQuery(c); query.setParameter("name", "employee_1"); List<Employee> result = query.getResultList(); ResultViewer.showResult(result, "criteria query : parameter query"); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * NOTE : Hibernate cannot support case in criteria way * // w w w .j a v a 2s . c om * case 1: * SELECT p.name, * CASE WHEN TYPE(p)=bq.jpa.demo.query.domain.DesignProject THEN 'dev' * WHEN TYPE(p)=bq.jpa.demo.query.domain.QualityProject THEN 'QA' * ELSE 'unknown' * END * FROM jpa_query_project p * * case 2: * SELECT p.name, * CASE TYPE(p) * WHEN bq.jpa.demo.query.domain.DesignProject THEN 'dev' * WHEN bq.jpa.demo.query.domain.QualityProject THEN 'QA' * ELSE 'unknown' * END * FROM jpa_query_project p */ @Transactional public void doWhere4() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Object[]> c = cb.createQuery(Object[].class); Root<Project> p = c.from(Project.class); // method 1 c.multiselect(p.get("name"), cb.selectCase().when(cb.equal(p.type(), DesignProject.class), "dev") .when(cb.equal(p.type(), QualityProject.class), "QA").otherwise("unknown")); // showResult(c); // method 2: CriteriaQuery<Object[]> c2 = cb.createQuery(Object[].class); Root<Project> p2 = c2.from(Project.class); c2.multiselect(p2.get("name"), cb.selectCase(p2.type()).when(DesignProject.class, "dev") .when(QualityProject.class, "QA").otherwise("unknown")); // showResult(c2); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * fetch join:/*from ww w . j av a2 s . c o m*/ * SELECT e FROM jpa_query_employee e JOIN FETCH e.address */ // @Transactional public void doFrom3() { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Employee> c = cb.createQuery(Employee.class); Root<Employee> e = c.from(Employee.class); e.fetch("address"); e.fetch("department"); e.fetch("projects", JoinType.INNER); // e.fetch("phones"); // e.fetch("directs"); // e.fetch("manager"); c.select(e); // only show the fetched data TypedQuery<Employee> query = em.createQuery(c); List<Employee> result = query.getResultList(); for (Employee emp : result) { System.out.println(emp.getId() + " | " + emp.getName() + " | " + emp.getAddress() + " | " + emp.getDepartment() + " | " + emp.getProjects()); } }
From source file:org.ow2.proactive.scheduling.api.graphql.fetchers.DatabaseConnectionFetcher.java
/** * Adaptation of the algorithm defined in the GraphQL specification. * <p>// ww w. ja v a 2 s . c om * Please look at the following link for more details: * https://facebook.github.io/relay/graphql/connections.htm#sec-Pagination-algorithm * <p> * The opaque cursor that is returned to the client makes use of the entity ID internally. */ protected ExtendedConnection createPaginatedConnection(DataFetchingEnvironment environment, Class<E> entityClass, Function<Root<E>, Path<? extends Number>> entityId, Comparator<E> entityComparator, BiFunction<CriteriaBuilder, Root<E>, List<Predicate[]>> criteria, CursorMapper<T, Integer> cursorMapper) { Integer first = environment.getArgument(FIRST.getName()); Integer last = environment.getArgument(LAST.getName()); Integer after = cursorMapper.getOffsetFromCursor(environment.getArgument(AFTER.getName())); Integer before = cursorMapper.getOffsetFromCursor(environment.getArgument(BEFORE.getName())); CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery<E> criteriaQuery = criteriaBuilder.createQuery(entityClass); Root<E> entityRoot = criteriaQuery.from(entityClass); Path<? extends Number> entityIdPath = entityId.apply(entityRoot); Predicate cursorPredicate = createCursorPredicate(criteriaBuilder, entityIdPath, after, before); int maxResults = applySlicing(criteriaQuery, criteriaBuilder, entityIdPath, first, last); CriteriaQuery<E> select = criteriaQuery.select(entityRoot); List<Predicate[]> predicates = criteria.apply(criteriaBuilder, entityRoot); Predicate[] wherePredicate = buildWherePredicate(predicates, cursorPredicate, criteriaBuilder); if (wherePredicate.length > 0) { select.where(wherePredicate); } TypedQuery<E> query = entityManager.createQuery(select); if (maxResults > -1) { query.setMaxResults(maxResults); } Stream<E> dataStream = query.getResultList().stream(); // if last is provided, reverse the stream // in order to get results sorted in ascending order based on entities ID if (last != null) { dataStream = dataStream.sorted(entityComparator); } Stream<T> data = dataMapping(dataStream); ExtendedConnection connection = createRelayConnection(entityManager, entityClass, criteriaBuilder, wherePredicate, cursorMapper, data, first, last); return connection; }
From source file:eu.uqasar.service.AbstractService.java
private List<T> getRange(int first, int count) { logger.infof("loading %d %s starting from %d ...", count, readableClassName, first); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> query = cb.createQuery(this.clazz); query.from(this.clazz); return em.createQuery(query).setFirstResult(first).setMaxResults(count).getResultList(); }