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

Using JPA 2.0 Criteria API and cast causes generated JPQL to fail in Hibernate

I am a first time user of the new JPA 2.0 Criteria API and I 'm running into a problem when I need to cast a number field to String to compare it with a String parameter. Reason is that I want to search for partial numbers, so I use a 'like' on the…
bruma
  • 101
  • 1
  • 6
7
votes
1 answer

how to left join two unrelated entities with JPA criteria api?

using JPA 2.1 and hibernate 5.1.x, this is possible with JPQL select s.lowerBound, l.status ... from Serie s left join Line l on s.lowerBound between l.lineStart and l.lineEnd how do i write this using Criteria api? i attempted this Root
kfc
  • 291
  • 7
  • 24
7
votes
1 answer

Date comparison using the JPA criteria API

I got a range date picker with two dates: start and end, where both can be empty. I would like to filter a table, where the entity has exact one date: date. So, here are some examples. I'd like to match. Imaging the date to match is the current date…
alexander
  • 1,191
  • 2
  • 20
  • 40
7
votes
2 answers

JPA 2 -- Using @ElementCollection in CriteriaQuery

@Entity public class Person { @ElementCollection private List locations; [...] } @Embeddable public class Location { private Integer dummy; private Date creationDate; …
nihilist84
  • 1,191
  • 3
  • 11
  • 20
7
votes
1 answer

Hibernate 5 and Typed Criteria Queries (JPA2)

Questions: 1) If I upgrade from Hibernate 4.x to Hibernate 5.x, can I still use the "old" Criteria Queries, or only the new Typed JPA2 Criteria Queries? Is the old one deprecated, or can I use both side by side? 2) Did I understand correctly that…
Marcelo Glasberg
  • 29,013
  • 23
  • 109
  • 133
7
votes
6 answers

EntityManager.find can't find entity, but using the Criteria API does

I've encountered a rather odd case in Java EE 6 where using the JPA EntityManager's find method along with an entity's primary id returns null, but using the Criteria API to select all entities with that id works fine. Here is the code I'm using for…
cdmckay
  • 31,832
  • 25
  • 83
  • 114
7
votes
2 answers

Subquery in where clause with CriteriaQuery

Can anybody give me some hints on how to put that kind of subquery in a CriteriaQuery? (I'm using JPA 2.0 - Hibernate 4.x) SELECT a, b, c FROM tableA WHERE a = (SELECT d FROM tableB WHERE tableB.id = 3) - the second select will always get a single…
artaxerxe
  • 6,281
  • 21
  • 68
  • 106
7
votes
1 answer

How does one construct multi-column "WHERE ... IN" expressions using JPA's criteria API?

For an Entity with a multi-column ID, we need to persist all those objects in a given List that aren't already present in the DB. Since the number of objects to check is large, and the number of objects in storage can be extremely large, the idea is…
Daniel
  • 75
  • 4
7
votes
1 answer

CriteriaBuilder boolean comparison

I'm currently doing like this. final CriteriaBuilder builder = ...; final boolean flag = ...; if (flag) { builder.isTrue(expression); } else { builder.isFalse(expression); } Can I use it like this? builder.equals(expression, flag); Is…
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
7
votes
3 answers

CriteriaBuilder - Sum using SelectCase

I am trying to perform a summation SQL query like the following: select group_ID, sum(case when user_type = 'Exec' then 1000 when user_type = 'Office' then 10 else 0 end) from subscription group by group_ID; …
user1186233
  • 157
  • 1
  • 1
  • 11
6
votes
3 answers

How to properly cast string to number with JPA2 Criteria API?

I am trying to write a query with subselect where a string is cast to a long. I'm probably missing something? Query looks like: CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery query = cb.createQuery(Task.class); Root from =…
Martynas Jurkus
  • 9,231
  • 13
  • 59
  • 101
6
votes
2 answers

Mock a JPA CriteriaBuilder with Mockito

I have a particularly nasty JMock checking() block for a JPA query that I want to migrate to Mockito: Mockery jMock = new Mockery(); final EntityManager fakeEntityManager = jMock.mock(EntityManager.class); final CriteriaBuilder fakeCriteriaBuilder =…
seanhodges
  • 17,426
  • 15
  • 71
  • 93
6
votes
3 answers

Oracle Text Criteria Query in JPA

Is it possible to perform a JPA Criteria Query using Oracle Text's contains statement, and if so how?
Ryan
  • 7,499
  • 9
  • 52
  • 61
6
votes
2 answers

Some basic questions on Criteria from JPA 2.0

I discovered JPA 2.0 Criteria API today and want to learn it. Just went through some examples and try to do a hands on. I have a table fruit with columns: id, name, color, size, taste. The regular stuff: EntityManagerFactory emf =…
Vicky
  • 16,679
  • 54
  • 139
  • 232
6
votes
1 answer

CASE statement in HQL or Criteria

derived from this question, is it possible to use HQL or Criteria for the following SQL statement: SELECT e.type, count(e), count(d), count (case when gender = 'male' then 1 else NULL end) AS NumberOfMaleEmployees from Department d…
Clayton Louden
  • 1,056
  • 2
  • 11
  • 28