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
7 answers

Hibernate Criteria API - adding a criterion: string should be in collection

I have to following entity object @Entity public class Foobar { ... private List uuids; ... } Now I'd like to make a criteria query which would fetch all Foobar pojos whose uuids list contains the string "abc123", I'm just…
KCL
  • 6,733
  • 10
  • 37
  • 43
6
votes
2 answers

Criteria API: filter by class type

I'm relativley new to relational databases and I have some problems concerning the creation of queries. First I want to explain the situation shortly. I have several entity classes. All of them extend AbstractEntity or EntityProperty. So entities…
Jogi
  • 295
  • 3
  • 11
6
votes
2 answers

Example using countDistinct in a JPA Criteria API query

I'm having trouble figuring out how to represent the following JPQL query: SELECT count(e) FROM Foo e using Criteria API. What I'm trying is: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery c = cb.createQuery(Foo.class); Root
Tim
  • 6,851
  • 11
  • 42
  • 46
6
votes
1 answer

JPA2 Criteria API runtime cast from varchar(25) to decimal

So I've seen all threads on stack overflow on similar subjects but I've found no solution to my problem. I'm trying to create a Criteria query and I get this SQL (1st SQL, simplified): SELECT latitude FROM stations WHERE (ABS(latitude - 45.893227)…
Denis K
  • 150
  • 1
  • 9
6
votes
1 answer

How to use CriteriaQuery SUM of custom operation on some cells?

Consider you have table T, with fields A and B. With regular SQL, I could do this: SELECT SUM(A * (100.0 - B) / 100.0) AS D FROM T; And I would get exactly what I expect. However, I'm not sure how to do it with CriteriaQuery. I know how to do sum…
ioreskovic
  • 5,531
  • 5
  • 39
  • 70
6
votes
1 answer

JPA Criteria Subquery on JoinTable

How do I create an efficient JPA Criteria query to select a list of entities only if they exist in a join table? For example take the following three tables: create table user (user_id int, lastname varchar(64)); create table workgroup…
Ryan
  • 7,499
  • 9
  • 52
  • 61
6
votes
1 answer

JPA criteria query in a many-to-many relationship

I'm using JPA 2.0 in EclipseLink 2.3.2 in which I have a many-to-many relationship between products and their colours. A product can have many colours and a colour can be associated with many products. This relationship is expressed in the database…
Tiny
  • 27,221
  • 105
  • 339
  • 599
6
votes
1 answer

JPA 2.0 subselect / subquery in order by clause with criteria api

I want to use JPA 2.0 criteria api to build the order by clause with a subselect. I know that you can do that in plain SQL but can it be mapped with criteria api? Can someone please give a code example? Example: Order(name, address) //…
user2912612
6
votes
0 answers

Criteria Query with BigMoney from Joda Money (Multi-Column field in Entity)

I have a serious issue with Criteria Query. My entity class looks like this: class X { ... @Columns(columns = { @Column(name = "priceCurrency", nullable = false), @Column(name = "priceAmount", nullable = false)}) @Type(type =…
Benjamin M
  • 23,599
  • 32
  • 121
  • 201
6
votes
2 answers

JPA 2 Criteria API case insensitive condition without metamodel usage

How to create Case-insensitive Criteria Query without metamodel usage? I'm trying to do something like this: CriteriaBuilder builder = entityManager.getCriteriaBuilder(); CriteriaQuery criteria =…
kostepanych
  • 2,229
  • 9
  • 32
  • 47
6
votes
2 answers

Having clause along with 'case when' in CriteriaBuilder

I want to build the having clause shown below using CriteriaBuilder : select objectid, sum(case when attr_meta = 'severity' then 1 else 0 end) as severity, sum(case when attr_meta = 'priority' then 1 else 0 end) as priority from object d group…
Nikhil
  • 93
  • 1
  • 2
  • 6
6
votes
1 answer

Spring JPA: Query builder versus Criteria Builder , which one to use?

I am going to start new project that would play with very large database, thus would be having lots of database operation. I have decided to use Spring JPA as my ORM. Now Spring JPA provides various techniques to use in DAO layer. I am confused…
Dhruv
  • 10,291
  • 18
  • 77
  • 126
6
votes
1 answer

Criteria API correlate

I have been Googling but do not understand what the consequence of calling the method correlate of javax.persistence.criteria.Subquery en the Criteria API. http://www.objectdb.com/api/java/jpa/criteria/Subquery/correlate_CollectionJoin_ This is…
pethel
  • 5,397
  • 12
  • 55
  • 86
6
votes
1 answer

JPA Criteria select all instances with max values in their groups

Is there a way to write with JPA 2 CriteriaBuilder the equivalent of the following query? select * from season s1 where end = ( select max(end) from season s2 where s1.contest_id=s2.contest_id ); In JPQL this query is: Select s1 from…
Ivan Sopov
  • 2,300
  • 4
  • 21
  • 39
6
votes
3 answers

JPA + Hibernate count(*) using CriteriaBuilder - with generatedAlias

When trying to create a count(*) type query using CriteriaBuilder I get the below alias problem. What changes should I make to the code below to get the count? Constraints: I have to use CriteriaBuilder/Query as the where clause has to be built…
Espresso
  • 5,378
  • 4
  • 35
  • 66