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
46
votes
3 answers

JPA Criteria API: how to express literal true and literal false?

How can I express literal true and literal false in JPA's Criteria API? I'm looking for something like Predicate alwaysTrue = CriterialBuilder.DefaultLiterals.TRUE (analogous to java.lang.Boolean.TRUE).
Abdull
  • 26,371
  • 26
  • 130
  • 172
45
votes
5 answers

JPA Criteria API with multiple parameters

I need to make a search method that uses the JPA Criteria API with multiple parameters. Now the problem is that not every parameter is required. So some could be null, and they shouldn't be included in the query. I've tried this with the…
mokuril
  • 742
  • 2
  • 9
  • 14
37
votes
3 answers

JPA - Criteria API and EmbeddedId

I want to use criteria to make the following query. I have an Entity with EmbeddedId defined: @Entity @Table(name="TB_INTERFASES") public class Interfase implements Serializable { @EmbeddedId private InterfaseId id; } @Embeddable public…
Mauro Monti
  • 1,068
  • 2
  • 12
  • 21
35
votes
1 answer

Select MAX timestamp with JPA2 Criteria API

So my entity has: @Column(name="TS", nullable=false) private java.sql.Timestamp timestamp; My generated MetaModel has: public static volatile SingularAttribute timestamp; I want to select by the Max Timestamp…
planetjones
  • 12,469
  • 5
  • 50
  • 51
33
votes
3 answers

Using the JPA Criteria API, can you do a fetch join that results in only one join?

Using JPA 2.0. It seems that by default (no explicit fetch), @OneToOne(fetch = FetchType.EAGER) fields are fetched in 1 + N queries, where N is the number of results containing an Entity that defines the relationship to a distinct related entity.…
Shaun
  • 2,490
  • 6
  • 30
  • 39
31
votes
1 answer

JPA Criteria Query distinct

I am trying to write a distinct criteria query, using: CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery query = builder.createQuery(RuleVar.class); Root ruleVariableRoot =…
Greg
  • 1,339
  • 2
  • 13
  • 18
30
votes
6 answers

How to join unrelated entities with the JPA Criteria API

Two database tables have a foreign key relationship. They are mapped to two entities A and B by JPA, but the join columns are manually removed from the entities, so in JPA world classes A and B are not related and you cannot navigate from one to…
Filippo
  • 1,123
  • 1
  • 11
  • 28
29
votes
5 answers

JPA Named Queries vs Criteria API?

Is there a heuristic/best practice/ruleset for a decision between the Criteria API and NamedQuery? My thoughts so far : Named queries are generally more readable. Criteria queries are more flexible. Both are precompiled. I tend to rely on using…
kostja
  • 60,521
  • 48
  • 179
  • 224
29
votes
2 answers

OpenJPA criteriaBuilder nested object property fetch

Is there any way in OpenJPA to get hold of a nested object property via CriteriaBuilder? Here's a small case. @Entity public class X { private Object Y; // getters, setters... } @Entity public class Y { private String Z; …
quantum
  • 3,000
  • 5
  • 41
  • 56
29
votes
9 answers

JPA/Hibernate Static Metamodel Attributes not Populated -- NullPointerException

I would like to use JPA2 Criteria API with metamodel objects, which seems to be pretty easy: ... Root albm = cq.from(JPAAlbum.class); ... albm.get(JPAAlbum_.theme) ... ; but this Root.get always throws a NullPointerException.…
Kevin
  • 4,618
  • 3
  • 38
  • 61
27
votes
3 answers

JPA CriteriaBuilder Subquery multiselect

I have a question about Subquery class in jpa. I need to create subquery with two custom field, but subquery doesn't have multiselect method and select method has Expression input parameter(In query this is Selection) and constact method not…
grinder
  • 436
  • 1
  • 6
  • 10
26
votes
2 answers

JPA Criteria API IN expression Parameter list

Is there a possibility to use a parameter list in Criteria API .in expression? I have something like this: List list = new ArrayList(); list.add((long)1); list.add((long)2); list.add((long)3); CriteriaBuilder cb =…
user1414341
  • 321
  • 1
  • 4
  • 6
25
votes
4 answers

Using Java generics for JPA findAll() query with WHERE clause

So, After a 10+ year break I'm coming back to Java and trying out stuff with JPA and Java generics. I've created a generics based findAll(other) JPA query that basically does SELECT * FROM source WHERE other_id = other.id; This is where I'm up to.…
Jim Richards
  • 708
  • 1
  • 7
  • 19
25
votes
3 answers

Criteria JPA 2 with 3 tables

I'm trying to create a criteria to retrieve some objects from 3 tables (Associate, Update and Detail). A Detail has reference to Associate and Update, and an Update has reference to a list of Details. My objective is to retrieve a list of Updates…
Rafael Toledo
  • 5,599
  • 6
  • 23
  • 33
25
votes
2 answers

JPA criteria API - Matching against a list in Spring Data JPA Specifications

I'm want to create a specification that matches a group id of a user object against a list of ids. I was thinking about using isMember (like in the code) but the method won't take the list. public static Specification matchCompanyIdsList(final…
levtatarov
  • 1,632
  • 6
  • 24
  • 36