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

How to filter child entities collections with predicate?

I have an entity service on which I need to filter a collection of child entity, based on a list of id's. My service have a public method which receive the id of the parent entity and a list of id's of some of his children entities. By default, I…
Mario Brissette
  • 131
  • 1
  • 1
  • 9
9
votes
2 answers

Delete multiple objects at once using CriteriaBuilder

I'm trying to delete a bunch of objects with one query using the CriteriaBuilder API. I'm looking for something like this select: CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder(); CriteriaQuery query =…
Jorn
  • 20,612
  • 18
  • 79
  • 126
9
votes
1 answer

Dynamic JPA 2.0 query using Criteria API

I am a bit stucked constructing a dynamic query using the CriteriaBuilder of JPA 2.0. I have quite a common use case I guess: User supplies a arbitrary amount of search parameters X to be and / or concatenated: like : select e from Foo where (name…
Ben
  • 185
  • 2
  • 8
9
votes
3 answers

Using JPA Criteria Api and hibernate spatial 4 together

Given the query example here: http://www.hibernatespatial.org/tutorial-hs4.html Query query = em.createQuery("select e from Event e where within(e.location, :filter) = true", Event.class); query.setParameter("filter", filter); Is it possible to…
osh
  • 1,191
  • 4
  • 13
  • 21
9
votes
2 answers

Correct usage of JPA criteria API, Predicates and where method of CriteriaQuery

I am trying to test a JPA repository. Here is my client/test code: @Test public void testFindBoitesByMultiFieldWithIdentifiantSet() { BoiteDataOnDemand dod = new BoiteDataOnDemand(); Boite boite = dod.getSpecificBoite(0); …
balteo
  • 23,602
  • 63
  • 219
  • 412
9
votes
2 answers

Spring data JPA Specifications - @OneToMany dependency

i have a problem with getting List from entity Person using Spring data JPA specifications (because of pagination). I need to get all notes by person but dependency between these two entities is on Person side. I don't know how to create my…
Zdend
  • 591
  • 3
  • 7
  • 19
9
votes
2 answers

JPA criteria query load entire table

I feel like this is a silly question, but I can't find the answer. I have a class as follows: import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import…
Greg
  • 1,339
  • 2
  • 13
  • 18
9
votes
3 answers

How to query an M:N relationship with JPA2?

I have an an object (BlogPost) that contains an M:N collection of elements (Tags). How to query for an object (BlogPost) where at least one it its Tags matches an element in a set of Tags (defined by the user) with JPA2…
Pumuckline
  • 627
  • 8
  • 17
9
votes
1 answer

JPA Criteria query Path.get left join is it possible

I have a question regarding JPA criteria. Here is my JPA criteria query: CriteriaBuilder criteriaBuilder = getEm().getCriteriaBuilder(); CriteriaQuery query = criteriaBuilder.createQuery(InventoryItemSumReport.class); …
landal79
  • 1,520
  • 1
  • 11
  • 26
8
votes
1 answer

What is CriteriaBuilder's literal() method for?

The documentation says: Create an expression for a literal In the code I see such uses of cb.literal(): Expression wordLiteral = cb.literal(word); predicates.add(cb.like(namePath, wordLiteral)); But if omit wordLiteral here and use…
Ekaterina
  • 1,642
  • 3
  • 19
  • 36
8
votes
2 answers

Tuple result Criteria API subquery

I am trying to use subqueries in an application I am writing using JPA 2.0 type-safe criteria API, with Hibernate 3.6.1.Final as my provider. I have no problem selecting primitive types (Long, MyEntity, etc.), but I want to select multiple…
logan
  • 3,416
  • 1
  • 33
  • 45
8
votes
1 answer

Criteria builder group by and having count does not work

This is the query i want to achieve: SELECT * FROM ROOMENTITY R JOIN CALENDARENTITY C ON R.id = C.room_id WHERE C.available = TRUE AND C.date BETWEEN :checkin AND :checkout GROUP BY C.room_id HAVING COUNT(C.room_id)>= diffdays This…
peterthunder
  • 201
  • 2
  • 7
8
votes
1 answer

JPA criteria query, order on class

Is there a way with JPA criteria queries to order on class? Imagine the following domain objects: abstract class Hobby { ... } class Coding extends Hobby { ... } class Gaming extends Hobby { ... } Using regular QL I was able to do from Hobby h…
Jeroen
  • 527
  • 2
  • 7
  • 19
8
votes
2 answers

JPA CRITERIA QUERY with order by joined columns

How to invoke order by on a joined entity? I am trying to achieve the following with: select * from person p inner join telephone t on p.id=t.person_id join sim s on s.id=t.sim_id order by s.name DESC @Entity public class Person implements…
Wait
  • 149
  • 1
  • 3
  • 12
8
votes
4 answers

Java criteria builder query not null or empty

I'm trying to check if a column in the db is not an empty string or not null but I can't figure out how to do it with criteria builder queries in order to get actual objects back. This sql works: sampleName is not null and sampleName != '' But…
Crystal
  • 28,460
  • 62
  • 219
  • 393