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
8
votes
1 answer

Generating correct and or condition with JPA Criteria API

I am trying to generate query with multiple brackets in 'or' and 'and' conditions but internal brackets are not getting generated. Brackets for outer predicates are generating properly but not for internal ones. Code: CriteriaBuilder…
bpsingh
  • 363
  • 1
  • 4
  • 18
8
votes
3 answers

What is the JPA CriteriaBuilder way for filtering on Sub Classes?

JPA 2.0 provided a means to filter by subclass using the JPQL expressions TYPE, for example : SELECT e FROM entity e WHERE TYPE(e) = :entityType where parameter entityType would be the value of the discriminator column. What is the recommended way…
Grant Lay
  • 800
  • 7
  • 17
8
votes
4 answers

How to select just the foreign key value using Criteria Query?

Suppose I have two entities as: @Entity public class A { @Id private int id; @ManyToOne private B b; //more attributes } @Entity public class B { @Id private int id; } So, the table for A is having a column as b_id…
Anmol Gupta
  • 2,797
  • 9
  • 27
  • 43
8
votes
1 answer

How to create specification using JpaSpecificationExecutor by combining tables?

I am using JpaSpecificationExecutor for creating custom queries. How do I create a Specification for the following SQL? select * from employee e, address a where e.id=23415 and e.name="Foo" and a.city="London"; Java Class : public static…
DarkCrow
  • 785
  • 2
  • 8
  • 29
8
votes
2 answers

JPA Expression concatenate more than two columns

I have the following statement to concatenate two columns which works well Expression stringConcat = cb.concat(cb.concat(root.get(Employee_.userId), " # "), …
Jacob
  • 14,463
  • 65
  • 207
  • 320
8
votes
3 answers

Hibernate Criteria query Collections contains certain elements

I have some problems with Criteria. I have a Job class that contains a set of Skills. The problem comes when I want to filter jobs that contain 2 skills, for example, all jobs that contains skill with id 1 and 3. Now I have this: for (Skill skill :…
maracili
  • 181
  • 1
  • 3
  • 15
8
votes
2 answers

JPA Criteria Query over an entity hierarchy using single table inheritance

Say I have the following entities: @Entity @Inheritance(strategy = SINGLE_TABLE) @DiscriminatorColumn(name = "type") public abstract class BaseEntity { private Date someDate; private Date otherDate; private boolean…
Markus Yrjölä
  • 859
  • 1
  • 12
  • 25
8
votes
1 answer

get max value of column using jpa criteria query

I want to get the maximum value of column relationId from table ElementRelationType I have written code but its giving error CriteriaBuilder cb1 = entityManager.getCriteriaBuilder(); CriteriaQuery cq1 =…
Surya
  • 416
  • 4
  • 7
  • 26
8
votes
2 answers

In jpa criteria, "in case there is at least 1 row return true"

I'm trying to create the follow sentence using the criteria api in JPA (eclipselink), it simple ask if there exist some user in some category The sentence I want: SELECT CASE WHEN EXISTS (SELECT * FROM user WHERE category = ?) …
Troncador
  • 3,356
  • 3
  • 23
  • 40
8
votes
2 answers

NullPointerException at org.hibernate.ejb.criteria.path.AbstractPathImpl.get

This works fine: public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { return root.get("campState").get("statusId").in(campStatus); } but I change to : return …
samung88
  • 121
  • 1
  • 5
8
votes
2 answers

JPA Criteria API: Multiple condition on LEFT JOIN

I have a join reference like following for which the first join expression is constructed by the JPA API automatically. CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery c = cb.createTupleQuery(); Root demands =…
yesil
  • 697
  • 1
  • 9
  • 19
7
votes
2 answers

How to create bogus Predicate in JPA

Is there any way to create bogus Predicate in JPA? Sort of like this: CriteriaBuilder cb = em.getCriteriaBuilder; Predicate pred = cb.isTrue(false); Almost all the methods of CriteriaBuilder take Expression as parameter. I also tried this to no…
jFrenetic
  • 5,384
  • 5
  • 42
  • 67
7
votes
1 answer

How to sort by fetched properties with distinct JPA Specifications

I use Spring Boot 1.5.3.RELEASE and for me it's unclear how to sort by properties of nested objects with distinct and Specifications because of: Caused by: org.postgresql.util.PSQLException: ERROR: for SELECT DISTINCT, ORDER BY expressions must…
Roman Danilov
  • 351
  • 2
  • 14
7
votes
1 answer

Build predicates for a postgres jsonb column with criteria builder using JPA criteria

I need to add a predicate to my list of existing predicates for a JSONB column. Entity: @Entity @Table(name = "a") @TypeDefs({ @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class), }) public class EntityA { @Id …
Niv
  • 271
  • 1
  • 7
  • 16
7
votes
1 answer

Transform from Pageable.getSort() to List to sort a query made by Criteria API in a Spring repository custom

I've implemented one my custom Spring repository which receives in input a org.springframework.data.domain.Pageable object. This Pageable object has the following sort property (for example): id: DESC,name: ASC. I need to convert the…
Andrea Bevilacqua
  • 1,197
  • 3
  • 15
  • 27