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

JOIN with CriteriaQuery?

I'm trying do a JOIN using CriteriaQuery to get informations of my database but returns an exception about unable to resolve attribute and doesn't work. I'm looking for solution but still havent found. My code is…
FernandoPaiva
  • 4,410
  • 13
  • 59
  • 118
0
votes
2 answers

How to write the following MYSQL query in criteria query and hibernate query?

How can I write the criteria query and hibernate query for the following MySQL query SELECT * FROM (SELECT * FROM outdatadetail where algorithmno="a0025_d2" and stringno=01 ORDER BY testid desc) sub_query GROUP BY subjectid; Any suggestions.
Vinod
  • 2,263
  • 9
  • 55
  • 104
0
votes
0 answers

Criteria API, Agregrate function inside Agregate function , JPA 2.1

I am quite new to JPA, and Criteria API. Currently my goal is to get the max() of a count() agregate. Criteria code: CriteriaBuilder builder = em.getCriteriaBuilder(); CriteriaQuery criteriaQ = builder.createQuery(); Root s =…
MikeNQ
  • 653
  • 2
  • 15
  • 29
0
votes
1 answer

JPA Criteria API - how to select like "field not in"

I have the following situation. public class TestExecution { private Collection testExecutionTags; } public class TestExecutionTag { private Tag tag; } public class Tag { private String name; } What I now…
jholusa
  • 158
  • 1
  • 8
0
votes
1 answer

JPA Criteria API calling SQL Server database exist function

I am building a query using the JPA's CriteriaBuilder to call the SQL Server exist function to find data based on an XML field and it is failing to run due to a The argument 1 of the XML data type method "exist" must be a string literal. error. I…
eberg
  • 33
  • 3
0
votes
1 answer

JPA convert Expression to Expression

I am trying to do the following (cb = CriteriaBuilder): cb.between( cb.currentTimestamp(), kampanjeArtikkelPriser.get(KampanjeArtikkelPris_.overstyrtSalgsprisPeriodeFra), …
Lars Juel Jensen
  • 1,643
  • 1
  • 22
  • 31
0
votes
1 answer

hibernate criteria api how to get Path from fetched records?

I have a query written in criteria api that goes like this: CriteriaQuery query = builder.get().createQuery(Application.class).distinct(true); Root root = query.from(Application.class); root.fetch(Application_.answerSets,…
Lukas Novicky
  • 921
  • 1
  • 19
  • 44
0
votes
1 answer

jpa criteria api fetch on id

I got a code in my bean like this: Root root = query.from(Application.class); Fetch fetch = root.fetch(xxx.yyy, JoinType.LEFT); fetch.fetch(yyy_.location, JoinType.LEFT); fetch.fetch(yyy_.project,…
Lukas Novicky
  • 921
  • 1
  • 19
  • 44
0
votes
0 answers

Issue when using ORDER BY clause in criteria builder

We add the ORDER BY clause in criteria builder as bellow. criteriaQuery.orderBy(criteriaBuilder.asc(request.get("name")), criteriaBuilder.asc(type.get("val"))); When we see the query it looks like ORDER BY p.named.val But we expect ORDER BY…
Java-Seekar
  • 1,720
  • 5
  • 30
  • 52
0
votes
0 answers

I know can't use Criteria query with UNION but is there any way in Hibernate or JPA?

I have a situation where I have to create dynamic sorting, so this purpose I am using criteria query in JPA (Using predicate and all), (I can not use NamedQuery just because if i have to provide multple ways of sorting I ahve to provide multiple…
gahlot.jaggs
  • 1,083
  • 3
  • 11
  • 21
0
votes
1 answer

How to join 2 tables based on passing a parameter for search operation?

I have 2 entities Addemp and Job.I want to join these 2 tables based on empid .empid is foreign key in job table and primary key in addemp table here i am doing a search operation based on employee id .i am using criteria builder for search…
user3227175
  • 29
  • 1
  • 10
0
votes
1 answer

Order by MAX of 2 dates using hibernate criteria squery

I have a table in my database with both a CREATED_DATE and a MODIFIED_DATE. I'm hoping to do something like select , CASE WHEN MODIFIED_DATE IS NULL THEN CREATED_DATE ELSE MODIFIED_DATE END as editDate FROM TABLE ORDER BY…
albertlockett
  • 204
  • 3
  • 13
0
votes
1 answer

How best to model Hibernate Criteria Query

I have a pseudo sql query which looks like this: from TableA as a join fetch TableB as b WHERE (a.id, a.revision) IN ( SELECT id, MAX (revision) FROM TableA GROUP BY id) and (b.id, b.revision) IN ( SELECT id, MAX (revision)…
dasPing
  • 337
  • 1
  • 2
  • 19
0
votes
2 answers

Converting a HQL-query which includes elements() to Criteria API

I'm having trouble converting the following HQL-query to Criteria API and I was wondering if I could get some help from you guys SELECT child FROM Foo AS child WHERE child IN (SELECT elements(obj.foos) FROM Bar AS obj WHERE obj.id = ?) In other…
KCL
  • 6,733
  • 10
  • 37
  • 43
0
votes
0 answers

Select … not in equivalent in JPA2 criteria

Is there any way to perform a query like the following using JPA2 criteria APIs? select a from b where a not in (1, 2, 3, 4) This question is related to this one, this is the inverse.
Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127