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

JPA/Metamodel: Strange (inconsistent ?) example in Sun Docs

In Sun Online resources, they provide son example about the usage of the Criteria/Metamodel API, but as far as I understand Java, it seems to be impossible to work: CriteriaQuery cq = cb.createQuery(Pet.class); Metamodel m =…
Kevin
  • 4,618
  • 3
  • 38
  • 61
14
votes
2 answers

SELECT DISTINCT + ORDER BY in JPA 2 Criteria API

I've a class Lawsuit, that contains a List, each one with a Date attribute. I need to select all the Lawsuits ordered by the date of their Hearings I've a CriteriaQuery like CriteriaBuilder cb =…
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
14
votes
2 answers

JPA Criteria API: LEFT JOIN for optional relationships

I'm using the Criteria API basically the first time. It's about abstracting queries for a generic builder: public TypedQuery newQuery( Manager manager ) { CriteriaBuilder builder = this.entityManager.getCriteriaBuilder(); Class
Kawu
  • 13,647
  • 34
  • 123
  • 195
14
votes
1 answer

Does Hibernate Criteria Api completely protect from SQL Injection

I am working with Hibernate to protect my website from SQL Injection. I heard that Hibernate Criteria API is more powerful than HQL. Does Hibernate Criteria Api completely protect from SQL Injection?
ѕтƒ
  • 3,547
  • 10
  • 47
  • 78
14
votes
1 answer

JPA-style Criteria/CriteriaBuilder queries from Hibernate Session

I have an application that uses Hibernate 4.x, and it is currently using the native Hibernate APIs (meaning that I have a SessionFactory and Sessions). I just noticed that the existing Criteria API is deprecated in favor of JPA's (superior) Criteria…
Adam Batkin
  • 51,711
  • 9
  • 123
  • 115
14
votes
1 answer

Building a query using NOT EXISTS in jpa criteria api

I have two tables named as table1 ,table2.Both the tables are having same no of fields.There is no relation between these two tables.My requirement is I want all the records in table1 which are not there in table2. So I have written a query using…
aaaa
  • 487
  • 4
  • 13
  • 24
13
votes
2 answers

How to fetch all data in one query

I have multiple entities that are queried via JPA2 Criteria Query. I am able to join two of these entities and get the result at once: CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder(); CriteriaQuery criteriaQuery =…
Stinnux
  • 181
  • 1
  • 1
  • 7
13
votes
4 answers

criteria api where 1<>1 clause

I want the query not to return any values. I can't just not query database, so I'd like to add some unreachable condition to predicates, something like 'where 1 <> 1'. But the CriteriaBuilder.equal() doesn't allow to do that. Is there any way to…
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
12
votes
1 answer

JPA Criteria API: How to select property in nested collection

I have a class Customer and CustomerDependant entities. Customer has many to many bi-directional relationship with its dependents. I need to find customers filtering by name and dependent name. It's done something like this in JPQL: select c join…
Otávio Garcia
  • 1,372
  • 1
  • 15
  • 27
12
votes
7 answers

JPA 2.0: count for arbitrary CriteriaQuery?

I am trying to implement the following convenience method: /** * Counts the number of results of a search. * @param criteria The criteria for the query. * @return The number of results of the query. */ public int…
blubb
  • 9,510
  • 3
  • 40
  • 82
12
votes
2 answers

Criteria API limit results in subquery

I'm trying to write a query similar to select * from Table a where a.parent_id in (select b.id from Table b where b.state_cd = ? and rownum < 100) using the Criteria API. I can achieve the query without the rownum limitation on the…
Danny
  • 7,368
  • 8
  • 46
  • 70
12
votes
2 answers

JPA criteria API query subclass property

I want to execute a query matching a specific subclass property, so I'm trying to use treat(). In this example I want: all subjects with name starting with 'a', or all subjects, which are persons, with last name starting with 'a' private…
Michele Mariotti
  • 7,372
  • 5
  • 41
  • 73
12
votes
3 answers

JPA 2.0 metamodel in Netbeans?

I've read that since version 6.9, Netbeans includes annotation processing support, a feature needed, for instance, to generate JPA 2.0 entities' metamodels. However, I couldn't find any examples or documentation that shows exactly how to do it. Have…
dariopy
  • 568
  • 1
  • 7
  • 18
12
votes
1 answer

When to use the select clause in the JPA criteria API?

Without using CriteriaQuery#select() : public List
getAddressOfManager(String designation, String name, String orderByColumn) { Boolean ascending = false; CriteriaBuilder cb = emanager.getCriteriaBuilder(); …
Prem
  • 121
  • 1
  • 1
  • 4
12
votes
2 answers

How to use CriteriaQuery for ElementCollection and CollectionTable

I have a very simple entity Product which has a code, name and tags. Tags are stored in another table (product_tag) with product_id and tag columns. I need to search for products with certain tags using CriteriaQuery. To give an example I want to…
hevi
  • 2,432
  • 1
  • 32
  • 51