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
87
votes
4 answers

JPA & Criteria API - Select only specific columns

I would like to select only specific columns (ex. SELECT a FROM b). I have a generic DAO and what I came up with is: public List getAll(boolean idAndVersionOnly) { CriteriaBuilder builder = manager.getCriteriaBuilder(); CriteriaQuery
BartoszCichecki
  • 2,227
  • 4
  • 28
  • 41
72
votes
2 answers

What to use: JPQL or Criteria API?

My Java application is using JPA for object persistence. The business domain is very simple (just three classes are persistent, with 3-5 properties in each). Queries are simple as well. The question is which approach I should use: JPQL or Criteria…
yegor256
  • 102,010
  • 123
  • 446
  • 597
68
votes
5 answers

JPA Criteria Query API and order by two columns

I'm stuck with a simple problem; struggling how to invoke order by on a joined entity. Essentially I am trying to achieve the following with JPA Criteria: select distinct d from Department d left join fetch d.children c left join fetch…
kmansoor
  • 4,265
  • 9
  • 52
  • 95
66
votes
4 answers

JPA 2.0, Criteria API, Subqueries, In Expressions

I have tried to write a query statement with a subquery and an IN expression for many times. But I have never succeeded. I always get the exception, " Syntax error near keyword 'IN' ", the query statement was build like this, SELECT t0.ID,…
Keating
  • 3,380
  • 10
  • 34
  • 42
65
votes
2 answers

JPA CriteriaBuilder - How to use "IN" comparison operator

Can you please help me how to convert the following code to using "in" operator of criteria builder? I need to filter by using list/array of usernames using "in". I also tried to search using JPA CriteriaBuilder - "in" method but cannot find good…
Jemru
  • 2,091
  • 16
  • 39
  • 52
63
votes
4 answers

Compare Date entities in JPA Criteria API

Using JPA 2 with EclipseLink implementation. I'm trying to build a dynamic query which should bring me some records persisted after a given date. CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery criteria =…
Ionut
  • 2,788
  • 6
  • 29
  • 46
58
votes
4 answers

Spring Data JPA. How to get only a list of IDs from findAll() method

I have a very complicated model. Entity has a lot relationship and so on. I try to use Spring Data JPA and I prepared a repository. but when I invoke a method findAll() with specification for the object a have a performance issue because objects are…
tomasz-mer
  • 3,753
  • 10
  • 50
  • 69
57
votes
4 answers

JPA Criteria API - How to add JOIN clause (as general sentence as possible)

I am trying to construct queries dynamically, and my next target is add JOIN clauses (I don't know how can I use the API). By now, for example, this code work for me : ... Class baseClass; ... CriteriaBuilder cb =…
Mandor
  • 575
  • 1
  • 5
  • 7
56
votes
5 answers

JPA/Criteria API - Like & equal problem

I'm trying to use Criteria API in my new project: public List findEmps(String name) { CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery c = cb.createQuery(Employee.class); Root emp =…
John Manak
  • 13,328
  • 29
  • 78
  • 119
51
votes
2 answers

What does an underscore concatenated to a class name mean?

I was reading the "Dynamic, typesafe queries in JPA 2.0" article and stumbled upon this example: EntityManager em = ... CriteriaBuilder qb = em.getCriteriaBuilder(); CriteriaQuery c = qb.createQuery(Person.class); Root p =…
stacker
  • 68,052
  • 28
  • 140
  • 210
49
votes
1 answer

"Not in" constraint using JPA criteria

I am trying to write a NOT IN constraint using JPA Criteria. I've tried something like this: builder.not(builder.in(root.get(property1))); though I know it will not work. In the above syntax, how can I add the collection / list against which…
P.S.
  • 637
  • 1
  • 6
  • 7
49
votes
5 answers

Really dynamic JPA CriteriaBuilder

I need to create a "real" dynamic JPA CriteriaBuilder. I get an Map with the statements. It looks like: name : John surname : Smith email : email@email.de ...more pairs possible Here is what i implement: CriteriaBuilder cb =…
Felix
  • 556
  • 1
  • 5
  • 12
48
votes
5 answers

How to make a CriteriaBuilder join with a custom "on" condition?

I want make a query where I join 2 tables, using the CriteriaBuilder. In MySQL the query I'm trying to make would look like this: SELECT * FROM order LEFT JOIN item ON order.id = item.order_id AND item.type_id = 1 I want to get all orders and if…
Bjørn Stenfeldt
  • 1,432
  • 1
  • 18
  • 25
47
votes
2 answers

Spring Data JPA: Creating Specification Query Fetch Joins

TL;DR: How do you replicate JPQL Join-Fetch operations using specifications in Spring Data JPA? I am trying to build a class that will handle dynamic query building for JPA entities using Spring Data JPA. To do this, I am defining a number of…
woemler
  • 7,089
  • 7
  • 48
  • 67
46
votes
2 answers

Distinct results from Spring Data JPA Specification that uses join

I have the following Specification that I use to query for any Contact entities that are tied to certain ManagedApplication entities. I pass in a Collection that contains the ids of the ManagedApplication entities that I am searching for.…
Andrew Mairose
  • 10,615
  • 12
  • 60
  • 102
1
2 3
99 100