Questions tagged [criteria-api]

This tag is for questions related to the Java Persistence Criteria API (from JPA 2.0) which is used to define queries through the construction of object-based query definition objects, rather than use of the string-based approach of the Java Persistence query language. For questions related to (N)Hibernate Criteria, use the [icriteria] tag.

Quoting the Overview from the JPA 2.0 Specification:

6.1 Overview

The Java Persistence Criteria API, like the Java Persistence query language is based on the abstract persistence schema of entities, their embedded objects, and their relationships as its data model. This abstract persistence schema is materialized in the form of metamodel objects over which the Criteria API operates. The semantics of criteria queries are designed to reflect those of Java Persistence query language queries.

The syntax of the Criteria API is designed to allow the construction of an object-based query “graph”, whose nodes correspond to the semantic query elements.

Java language variables can be used to reference individual nodes in a criteria query object as it is constructed and/or modified. Such variables, when used to refer to the entities and embeddable types that constitute the query domain, play a role analogous to that of the identification variables of the Java Persistence query language.

These concepts are further described in the sections that follow. The metamodel on which criteria queries are based is presented in Chapter 5. The static metamodel classes that can be used in constructing strongly-typed criteria queries are described in section 6.2. The javax.persistence.criteria interfaces are presented in Section 6.3. Sections 6.4 through 6.8 describe the construction and modification of criteria query objects. Additional requirements on the persistence provider are described in section 6.9.

1541 questions
22
votes
1 answer

spring-data subquery within a Specification

Spring-data, Oliver Gierke's excellent library, has something called a Specification (org.springframework.data.jpa.domain.Specification). With it you can generate several predicates to narrow your criteria for searching. Can someone provide an…
John Kroubalkian
  • 291
  • 1
  • 3
  • 11
21
votes
4 answers

How to do a distinct count in JPA critera API?

I would like to do this but with the criteria API instead: select count(distinct e) from Event e, IN(e.userAccessPermissions) p where p.principal = :principal and p.permission in (:permissions) Any ideas?
Piotr
  • 4,813
  • 7
  • 35
  • 46
21
votes
1 answer

Criteria Builder Create new Object In Select Statement

I was wondering if it's possible to create such query like : em.createQuery( "SELECT NEW EmpMenu(p.name, p.department.name) " + "FROM Project p ").getResultList(); also is it possible to do it via Specification: public Predicate…
user1827052
  • 247
  • 1
  • 3
  • 6
19
votes
3 answers

JPA 2 No explicit selection and an implicit one cold not be determined

I am trying to fetch all users for a folder where the user was created after a certain date. the relationship between the user and the folder lives in a separate table. This is the query I came up with but it throws the exception No explicit…
Farouk Alhassan
  • 3,780
  • 9
  • 51
  • 74
18
votes
1 answer

Using Projections in JPA 2

I need to convert a Hibernate criteria query like the following curList = session.createCriteria(Islem.class) .createAlias("workingDay", "d") .setProjection(Projections.sum("amount")) …
Skyhan
  • 871
  • 3
  • 15
  • 26
18
votes
1 answer

Using JPA2 criteria API without Metamodel on a List property

How can I formulate the following JPA2 criteria query without using the metamodel classes: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery cq = cb.createQuery(Employee.class); Root emp =…
jbandi
  • 17,499
  • 9
  • 69
  • 81
18
votes
4 answers

Can the same CriteriaBuilder (JPA 2) instance be used to create multiple queries?

This seems like a pretty simple question, but I have not managed to find a definitive answer yet. I have a DAO class, which is naturally querying the database by using criteria queries. So I would like to know if it is safe to use the same…
pkainulainen
  • 1,458
  • 1
  • 19
  • 51
17
votes
3 answers

How do I count the number of rows returned by subquery?

I want to do something like this: select count(*) from (select ...) (As it would be in SQL), but in JPA. Any ideas on how I would do it?
Piotr
  • 4,813
  • 7
  • 35
  • 46
17
votes
1 answer

JpaSpecificationExecutor JOIN + ORDER BY in Specification

I have a query using a JOIN and ORDER BY and want to use it within my repository using the Criteria Api. Here I found, how to wrap such a query into a CriteriaQuery (Link). CriteriaQuery cq = cb.createQuery(Pet.class); Root pet =…
aschi
  • 197
  • 1
  • 1
  • 8
16
votes
3 answers

Why Hibernate inlines Integer parameter list passed to JPA Criteria Query?

I am building a query using JPA Criteria API. When I created two restriction predicates using javax.persistence.criteria.Path#in(Collection) method the generated SQL query was a little bit different than I excpected. The first predicate which…
Lukas Risko
  • 1,435
  • 14
  • 25
16
votes
3 answers

Is there something like Restrictions.eq(true, false) in Criteria API?

I need a generic Criterion which forces the result to zero matches. Something like Restrictions.eq(true, false) ?
Christopher Klewes
  • 11,181
  • 18
  • 74
  • 102
16
votes
1 answer

CriteriaBuilder.and & CriteriaBuilder.or, how-to?

I'm trying to change the following HQL to use JPA Criteria: select distinct d from Department d left join fetch d.children c where d.parent is null and ( d.name like :term or c.name like :term ) order by d.name Department has a…
kmansoor
  • 4,265
  • 9
  • 52
  • 95
15
votes
2 answers

How to use JPA Criteria API when joining many tables

This is the further question to this: How to use JPA Criteria API in JOIN CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery criteria = criteriaBuilder.createQuery( Company.class ); Root companyRoot =…
Sami
  • 2,311
  • 13
  • 46
  • 80
14
votes
7 answers

How to properly determine whether an "exists" JPA Criteria Query clause returned true or false?

I don't know how to perform a JPA criteria query that returns with a boolean output. The goal is to have a criteria query that looks like this when rendered on Oracle: select 1 from dual where exists ( ... ); The where exists (...) part I performed…
Edy Bourne
  • 5,679
  • 13
  • 53
  • 101
14
votes
4 answers

JPA criteria API order by NULL last

I use JPA criteria API to fetch records from the datebase. I have entity Record with field dateTime which can be null. I would code: public List find(RecordFilter recordFilter, int page, int pageSize) { CriteriaBuilder criteriaBuilder =…
Yan Khonski
  • 12,225
  • 15
  • 76
  • 114