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

How to query for a M:N relationship using enums with CriteriaBuilder

I am curently working on a Company entity which contains a List of Area enum values. These are the areas, the company is working in. They are mapped as @ElementCollection. @Entity public class Company { @ElementCollection @CollectionTable(name…
Pumuckline
  • 627
  • 8
  • 17
6
votes
0 answers

JPA static metamodel with @MappedSuperclass (with a bit of Generation Gap Pattern)

I tried to achieve Generation Gap Pattern with JPA entities. Here is the solution we choose ( <-- are inheritance) BaseEntity <-- EntityGenerated <-- Entity The EntityGenerated type is abstract and mapped with @MappedSuperclass, all field are…
Yann Le Moigne
  • 612
  • 6
  • 16
6
votes
1 answer

Create a JPA Criteria fully dynamically

Usually I'm a Hibernate user and for my new project we use JPA 2.0. My DAO receives a Container with a generic. public class Container { private String fieldId; // example "id" private T value; // example new Long(100) T is a…
user1180339
  • 319
  • 1
  • 6
  • 16
6
votes
2 answers

If/Case statement in JPA Criteria Builder

I want to build up a query which search dates on different entites. My structure is: Contract has date (non nullable) Employee has date (non nullable) Employee may have a contract id (nullable) If a Employee has a contract I want to retrieve the…
RNJ
  • 15,272
  • 18
  • 86
  • 131
5
votes
1 answer

How to search on subclass attribute with a JPA CriteriaQuery?

I am trying to write a query to search a subclass attribute that is not in its superclass and return all objects of the superclass. Currently I am getting a NullPointerException when I try and do…
Ransom Briggs
  • 3,025
  • 3
  • 32
  • 46
5
votes
1 answer

Write HQL clause using Hibernate Criteria API

I want to write a method that returns a list of last added objects grouped by field 'serviceId'. The following HQL works, but I want to write this using Criteria API: FROM Notification WHERE date IN (SELECT MAX(date) FROM Notification GROUP BY…
elias
  • 15,010
  • 4
  • 40
  • 65
5
votes
2 answers

JPA 2 + Criteria API - Defining a subquery

I try to convert a sql query to Criteria API without success so far. I can create two separate queries which return the values I need, but I don't know how to combine them in a single query. Here is the sql statement which works: select company.*,…
Thaddel
  • 51
  • 1
  • 1
  • 3
5
votes
1 answer

NHibernate Criteria QueryByExample stuck with SQL in the middle

I am using Criteria to speed up a query, and I am almost there. Using Query By Example to match up rows in a table, remove duplicate rows with the same id, and then paginate. Of course I can't paginate until I remove the duplicate rows, and I don't…
5
votes
0 answers

How to perform subquery on an @ElementCollection within the same entity?

Consider the following entity. @Entity public class Member { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String memberName; @ElementCollection …
Qcumber
  • 130
  • 1
  • 2
  • 14
5
votes
1 answer

Criteria API combined AND / OR

I am struggling to create a query using the Criteria API. The following SQL query returns the result that I need: SELECT * FROM MODEL WHERE MANUFACTURER_ID = 1 AND SHORTNAME LIKE '%SF%' OR LONGNAME LIKE '%SF%'; I have written the following code…
Laurens
  • 2,078
  • 5
  • 29
  • 46
5
votes
2 answers

Call jsonb_contains function (postgres) using JPA criteria and JsonBinaryType

I have a JPA/Hibernate entity which has a JSONB column (using https://github.com/vladmihalcea/hibernate-types ) for storing a list of strings. This works fine so far. @TypeDef(name = "jsonb", typeClass = JsonBinaryType.class) @Type(type =…
DerM
  • 1,497
  • 1
  • 14
  • 27
5
votes
0 answers

Hibernate Criteria API is deprecated, but JPA Criteria API is complicated and almost unusable (e.g. for Joins)

My understanding is that the Hibernate Criteria API is deprecated: As of Hibernate 5.2, the JPA Criteria API should be used instead But in Hibernate Criteria API, building complex queries with JOINs was easy. I could do it as follows: Criteria…
gene b.
  • 10,512
  • 21
  • 115
  • 227
5
votes
1 answer

Group by date intervals using JPA's Criteria API

I'm trying to group entities by date intervals using JPA's Criteria API. I use this way of querying for entities as this is a part of the service that serves API requests which may ask for any field of any entity, including sorting, filtering,…
J_S
  • 2,985
  • 3
  • 15
  • 38
5
votes
0 answers

Joining with specific ON-clause with JPA 2 Criteria API

Is it somehow possible to do one of the following queries with JPA 2 Criteria API? Basicly, there are a number of Templates which are used by Profiles. Each User in the system has any number of Profiles, and two active ones. I'd like to get ALL…
Rasmus Franke
  • 4,434
  • 8
  • 45
  • 62
5
votes
1 answer

Spring Data JPA: CriteriaQuery to get entities with max value for each unique foreign key

There's an Event class: @Entity public class Event { @Id private Integer id; @ManyToOne(cascade = CascadeType.ALL) private Company company; @Column private Long time; ... } I want to have an EventFilter class…
Oleg Mikhailov
  • 5,751
  • 4
  • 46
  • 54