List of usage examples for javax.persistence.criteria CriteriaQuery from
<X> Root<X> from(Class<X> entityClass);
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:com.creditcloud.common.entities.dao.AbstractReadDAO.java
/** * list all entity//w w w . j av a 2s . co m * * @return */ public List<T> findAll() { EntityManager em = getEntityManager(); CriteriaQuery cq = em.getCriteriaBuilder().createQuery(); cq.select(cq.from(entityClass)); return em.createQuery(cq).getResultList(); }
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(); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * SELECT e FROM jpa_query_employee//from w w w.j a v a2 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' *///w w w . j ava 2 s .c om @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:eu.uqasar.service.AbstractService.java
protected List<T> getAllOrdered(Class<T> clazz, Order... orders) { logger.infof("loading all %ss ...", getReadableClassName(clazz)); CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<T> criteria = cb.createQuery(clazz); criteria.from(clazz); if (orders != null && orders.length > 0) { criteria.orderBy(orders);//from w ww .j a va 2s .co m } return em.createQuery(criteria).getResultList(); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * group by and having ://from w w w .j av a 2 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//from w w w . j a v a 2 s .com */ @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); }
From source file:com.creditcloud.common.entities.dao.AbstractReadDAO.java
/** * find entity in the range/*from w w w . j a v a 2s . c o m*/ * * @param range * @return */ public List<T> findRange(int[] range) { EntityManager em = getEntityManager(); CriteriaQuery cq = em.getCriteriaBuilder().createQuery(); cq.select(cq.from(entityClass)); Query q = em.createQuery(cq); q.setMaxResults(range[1] - range[0]); q.setFirstResult(range[0]); return q.getResultList(); }
From source file:bq.jpa.demo.query.criteria.service.CriteriaService.java
/** * multiple select// ww w .java 2 s.c om */ public void doSelect2() { CriteriaBuilder cb = em.getCriteriaBuilder(); // method1: tuple CriteriaQuery<Tuple> tupleQuery = cb.createTupleQuery(); Root<Employee> e1 = tupleQuery.from(Employee.class); tupleQuery.select(cb.tuple(e1.get("id"), e1.get("name"))); showResult(tupleQuery); // method2: object[] and criteriaquery.multiselec CriteriaQuery<Object[]> multiQuery = cb.createQuery(Object[].class); Root<Employee> e2 = multiQuery.from(Employee.class); multiQuery.multiselect(e2.get("id"), e2.get("name")); showResult(multiQuery); }