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

jpa criteria-api: join with subselect

This query is used to retrieve last records in a one-to-many relationship (see SQL join: selecting the last records in a one-to-many relationship) SELECT p.* FROM customer c INNER JOIN ( SELECT customer_id,…
user871611
  • 3,307
  • 7
  • 51
  • 73
12
votes
1 answer

truncate/delete from given the entity class

I have my entity class available via a method. I'm trying to figure out, how via the JPA JPQL or Criteria API's I could issue a truncate or delete from. I think that the criteria API is more natural for working with classes, and truncate is a faster…
xenoterracide
  • 16,274
  • 24
  • 118
  • 243
12
votes
1 answer

JPA2 Criteria: How to avoid a cross join using path.get()

If you have this entity: @Entity public class A { @ManyToOne @JoinColumn(name = "bField", nullable = true) private B myBObject; } And I have a generic generator of Criteria who will do that: Root root =…
user1180339
  • 319
  • 1
  • 6
  • 16
12
votes
3 answers

How to query for entities by their collection value

I'm using jpa and I have the following entity: @Entity @Table(name="favorites_folders") public class FavoritesFolder { private static final long serialVersionUID = 1L; @Id private String id; @NotNull @Size(min = 1, max =…
Noam
  • 3,049
  • 10
  • 34
  • 52
11
votes
4 answers

Need help creating JPA criteria query

I'm building my first Java EE web application using Glassfish and JSF. I'm fairly new to the criteria query and I have a query I need to perform but the javaee6 tutorial seems a little thin on examples. Anyway, I'm having a hard time creating the…
Alan B. Dee
  • 5,490
  • 4
  • 34
  • 29
11
votes
2 answers

Criteria API Path to sub-sub-entity id

I want to transform my JPAQL queries into criteria API queries. Consider query like this: SELECT e FROM Entity e WHERE e.parent.id = :parentId WHERE part is transformed to: Path parentId =…
amorfis
  • 15,390
  • 15
  • 77
  • 125
11
votes
1 answer

How to group by Case statement using JPA Criteria API / Hibernate

I am trying to perform a query like the following, with selecting by a case statement and grouping by the same case statement.. Select USER, (CASE WHEN value between 0 AND 2 then '0-2' WHEN value between 3 AND 4 then '3-4' ELSE '5+' …
TMitchell
  • 131
  • 1
  • 5
11
votes
2 answers

How to use native SQL as a fragment (where clause) of a bigger query made with Criteria API in Hibernate?

I have a following problem. In application, which I am developing, we use Hibernate and every query is written with Criteria API. Now, in some places, we want to add possibility for user to write some SQL code which will be used as part of where…
MichalD
  • 111
  • 1
  • 6
11
votes
4 answers

Get rid of redundant joins produced by subqueries in JPA criteria

I simply need to execute the following MySQL query using JPA criteria (fetching a list of states (from state_table) based on a country name given (in country)). SELECT state_id, state_name, country_id FROM state_table WHERE …
Tiny
  • 27,221
  • 105
  • 339
  • 599
11
votes
2 answers

How to refer to a subclass specific field in a CriteriaQuery where the super class is queried?

I'm trying to achieve something like the following, using the JPA Criteria API: SELECT b FROM Box b JOIN SpecialItem s WHERE s.specialAttr = :specialAttr The objects are Box @Entity public class Box implements Serializable { ... @ManyToOne …
Alex
  • 1,061
  • 2
  • 11
  • 19
11
votes
2 answers

Conditional where clause in JPA criteria query

I am facing an issue for JPA criteria query. How can I add multiple where clause in my Criteria Query with if else... My Requirement is: CriteriaBuilder builder = getEm().getCriteriaBuilder(); CriteriaQuery query =…
11
votes
3 answers

JPA Criteria API and type safety

I seem to have missed something in JPA's criteria API and its type safety. Consider the following code: @Entity @Access(FIELD) class User( @Id Long id; @Column(unique=true) String email; String password; } Here the meta…
John Smith
  • 3,037
  • 5
  • 29
  • 31
11
votes
2 answers

Criteria API: Fetch of a list returns repeated main entity

I have the following Entities; Ticket contains a set of 0,N WorkOrder: @Entity public class Ticket { ... @OneToMany(mappedBy="ticket", cascade = CascadeType.ALL, fetch = FetchType.LAZY) private List workOrders = null; …
SJuan76
  • 24,532
  • 6
  • 47
  • 87
11
votes
2 answers

Initializing a transient attribute of a JPA entity during CriteriaQuery

I'm wondering if it is possible to initialize a transient attribute of an entity during a criteria query. Example @Entity public SampleEntity{ @Id private long id; [more attributes] @Transient private String someTransientString; …
ftr
  • 2,105
  • 16
  • 29
10
votes
1 answer

JPA2 Criteria API .as(String.class) casting to char(1) - How do I work around this?

Using the criteria api, I have a query which does something like: CriteriaBuilder cb = entityManager.getCriteriaBuilder(); CriteriaQuery pathQuery = cb.createQuery(SourcePath.class); Root pathRoot =…
idbentley
  • 4,188
  • 3
  • 34
  • 50