How can I formulate the following JPA2 criteria query without using the metamodel classes:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
Root<Employee> emp = cq.from(Employee.class);
cq.where(cb.isEmpty(emp.get(Employee_.projects)));
cq.select(emp);
I would like to use:
cq.where(cb.isEmpty(emp.get("projects")));
But I cant figure out how to convert the Path to an Expression, which is needed by cb.isEmpty...
Thanks.
Try this:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Employee.class);
Root emp = cq.from(Employee.class);
cq.where(cb.isEmpty(emp.<List<Project>>get("projects")));
cq.select(emp);
Or, using a Path variable:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery cq = cb.createQuery(Employee.class);
Root emp = cq.from(Employee.class);
Path<List<Project>> projects = emp.get("projects"));
cq.where(cb.isEmpty(projects);
cq.select(emp);
Reference
javax.persistence.criteria API
<Y> Path<Y> Path.get(String):
Related
I have written a query in Criteria Builder, its looks like this.
private EntityManager entitymanager;
CriteriaBuilder criteriaBuilder = entitymanager.getCriteriaBuilder();
CriteriaQuery<Object> criteriaQuery =
criteriaBuilder.createQuery(Object.class);
Root<Employee> from = criteriaQuery.from(Employee.class);
criteriaQuery.orderBy(criteriaBuilder.asc(name));
TypedQuery<Object> typedQuery1 = entitymanager.createQuery(criteriaQuery );
List<Object> resultlist1 = typedQuery1.getResultList();
typedQuery1.setMaxResults(pageable.getPageSize());
After setMaxResults apply to the query, the order of the resultlist1 is
different as before?
Why is this happened?
How can I stop this behavior?
Why are you using Object for type? Use Employee
CriteriaQuery<Employee> criteriaQuery =
criteriaBuilder.createQuery(Employee.class);
Root<Employee> from = criteriaQuery.from(Employee.class);
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("column_name")));
TypedQuery<Employee> typedQuery1 = entitymanager.createQuery(criteriaQuery);
typedQuery1.setMaxResults(pageable.getPageSize());
List<Employee> resultlist1 = typedQuery1.getResultList();
Calling setMaxResults() after executing query does not make sense. Set max result before calling getResultList()
typedQuery1.setMaxResults(pageable.getPageSize());
List<Employee> resultlist1 = typedQuery1.getResultList();
What is name variable? Do order by like this:
criteriaQuery.orderBy(criteriaBuilder.asc(from.get("column_name")));
I've written following code by using Criteria Api:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<Entity> criteriaQuery = criteriaBuilder.createQuery(Entity.class);
Root<Entity> root = criteriaQuery.from(Entity.class);
criteriaQuery.select(root);
CriteriaQuery<Long> countQuery = criteriaBuilder.createQuery(Long.class);
countQuery.select(criteriaBuilder.count(root));
Long countOfRows = entityManager.createQuery(countQuery).getSingleResult();
As a result I get an exception:
java.lang.IllegalStateException: No criteria query roots were
specified
The exception is thrown by getSingleResult method (last line of the code). Thank you for help!
It works after forcing countQuery to add root:
((QueryStructure)((CriteriaQueryImpl)countQuery).queryStructure).roots.add(root)
I have the following SQL
SELECT ID,LASTTIMEEXECUTEDDATE as d FROM STATISTICSDATE ORDER BY LASTTIMEEXECUTEDDATE
which using CriteriaBuilder works just fine:
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<StatisticsDate> cq = cb.createQuery(StatisticsDate.class);
Root<StatisticsDate> rootEntry = cq.from(StatisticsDate.class);
CriteriaQuery<StatisticsDate> all = cq.select(rootEntry).orderBy(cb.desc(
rootEntry.get("lastTimeExecutedDate")));
TypedQuery<StatisticsDate> allQuery = em.createQuery(all);
However now i need to get more accurate results using this:
SELECT ID,LASTTIMEEXECUTEDDATE as d FROM STATISTICSDATE ORDER BY
to_timestamp(LASTTIMEEXECUTEDDATE, 'DD.MM.YYYY:HH24:MI:SS') desc;
I can do this via native sql BUT i would like to know if it is possible to use it via CriteriaBuilder.
What troubles me is to_timestamp(LASTTIMEEXECUTEDDATE, 'DD.MM.YYYY:HH24:MI:SS')
Thanks
Try it like this:
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<StatisticsDate> cq = cb.createQuery(StatisticsDate.class);
Root<StatisticsDate> rootEntry = cq.from(StatisticsDate.class);
CriteriaQuery<StatisticsDate> all = cq.select(rootEntry).orderBy(cb.desc(
cb.function(
"to_timestamp", Timestamp.class,
rootEntry.get("lastTimeExecutedDate"),
cb.literal("DD.MM.YYYY:HH24:MI:SS")
)
));
TypedQuery<StatisticsDate> allQuery = em.createQuery(all);
I've used hibernate for a long, now i started using JPA and i can't find a short way to write a simply select in less than seven lines (the use of criteria is a must in this project), is there a shorter way to build this query?
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Transaction> cq = cb.createQuery(Transaction.class);
Root<Transaction> root = cq.from(Transaction.class);
Collection<Predicate> predicates = new ArrayList<Predicate>();
predicates.add(cb.equal(root.get("originalOperationId"), originalOperationId));
cq.where(predicates.toArray(new Predicate[predicates.size()]));
List<Transaction> resultado = em.createQuery(cq).getResultList();
return resultado;
I'm trying to delete a bunch of objects with one query using the CriteriaBuilder API. I'm looking for something like this select:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaQuery<T> query = criteriaBuilder.createQuery(entityClass);
Root<T> root = query.from(entityClass);
query.select(root).where(/*some condition*/);
return entityManager.createQuery(query).getResultList();
but then a delete instead of a select. As far as I can see, there's no remove or delete method on CriteriaQuery. Is it possible using this API?
I can of course execute the select, then call entityManager.remove(object) for each result, but that feels very inefficient.
Try this:
CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
CriteriaDelete<SomeClass> query = criteriaBuilder.createCriteriaDelete(SomeClass.class);
Root<SomeClass> root = query.from(SomeClass.class);
query.where(root.get("id").in(listWithIds));
int result = entityManager.createQuery(query).executeUpdate();
The where clause can laso look like this:
query.where(criteriaBuilder.lessThanOrEqualTo(root.get("id"), someId));
in JPA 2.1, there are Criteria APIs exactly as what you want.it looks like this:
CriteriaBuilder cBuilder = em.getCriteriaBuilder();
CriteriaDelete<T> cq = cBuilder.createCriteriaDelete(entityClass);
Root<T> root = cq.from(entityClass);
cq.where(/*some codition*/);
int result = em.createQuery(cq).executeUpdate();
you can refert to JPA 2.1 SPEC and API here