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
0 answers

How to transform spring specification from one type to another?

Hello all, I am using Spring Specification in my application. In application I am having layered object design, So for each layer I am having a different DTO. e.g. For…
Sanjay Jain
  • 3,518
  • 8
  • 59
  • 93
0
votes
1 answer

Exception on select with in clause using hibernate JPA CriteriaQuery

I have this entity : @Entity(name="TestEntity") @Table(name = "TestTable") public class TestEntity { private Long id; ... some fields ... private List children; @ManyToMany(cascade = CascadeType.ALL) @Column …
Pooya
  • 4,385
  • 6
  • 45
  • 73
0
votes
2 answers

use criteria API to select * from where ... clause

I have two entities: Session and Formation. They have a common attribute idFormation. I want to select all sessions which have the same id as the formation using criteria API (the idFormation is the argument of my method) but at least I have an…
Betty
  • 67
  • 1
  • 8
0
votes
2 answers

No matching results for a unique query (error 782)

This code keeps throwing an error because there aren't any values present in the database: public Foo getFoo(Parent p1, Parent p2) { EntityManager entityManager = entityManagerFactory.createEntityManager(); CriteriaBuilder criteriaBuilder =…
user1191027
0
votes
0 answers

Java Criteria API join self referencing entity

I'm trying to do a nested join on a self referencing entity, please bear with me, it will make sense :) Thing.java class Thing { Integer something; Person owner; } Person.java class Person { String name; Person parent; } Note: A Person…
0
votes
1 answer

How to get the list of files, that has been recently updated or modified using JPA Criteria Query?

I am new to JPA Criteria Query.Table contains meta-data of the files and it may have multiple entries correspond to thefile name but it should have different-different last modified date in the Table. What I need is - a list of unique files from the…
Ashish Pancholi
  • 4,569
  • 13
  • 50
  • 88
0
votes
1 answer

How to retrive objects from one to many using HQL or Criteria API

Started to learn hibernate with examples. I have written a class "Team" that has one to many relationship with (a Collection of) players. I need to get all the teams where player name is "X" (X is not the primary key of the Player table) where…
bula
  • 8,719
  • 5
  • 27
  • 44
0
votes
0 answers

criteria query: indistinct result lists

I have 2 little problems. Let's say I have an entity class called MyEntity. This class has two kinds of properties. Therefore I have two different class Property1 and Property2 which are also entity classes. There are bidirectional relations…
Jogi
  • 295
  • 3
  • 11
0
votes
1 answer

Java criteria API - select * from table where id = id

I am new to the criteria api and am trying to build a select query where id matches id. I would like to build this: SELECT * FROM movie WHERE id = id(input var) So far I have this with an input variable id as a long: CriteriaBuilder…
legopiraat
  • 166
  • 1
  • 11
0
votes
0 answers

Use Criteria Query to get latest data record from database

How to get a latest record (row) from a database in JPA Criteria query? I have used something like that. But i cannot do that? CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery criteria = cb.createQuery(Number.class); …
Sameera Liaynage
  • 821
  • 2
  • 10
  • 12
0
votes
1 answer

How to exclude a collection with a JPA query

I'm creating a database query with JPA and need to exclude a list from the result. How should I do this? public List getPersonsWithoutNotWanted(List notWantedId ) { CriteriaBuilder cb = em.getCriteriaBuilder(); …
Dimitri Dewaele
  • 10,311
  • 21
  • 80
  • 127
0
votes
2 answers

Hibernate query with Criteria

Anyone please help me to convert the following SQL query to Hibernate: Select * from(select to_char(start_time, 'dd/mm/yyyy HH') st, sum(success_count) from GMO_DETAILS where start_time between ? and ? …
Ashwini
  • 63
  • 2
  • 8
0
votes
1 answer

Criteria Query select join

I need help to convert this JPQL query in Criteria Query: SELECT u.documentList FROM Unit u WHERE u.id = :id this is what i have tried: CriteriaQuery query = builder.createQuery(Document.class); Root root =…
Michele Mariotti
  • 7,372
  • 5
  • 41
  • 73
0
votes
1 answer

JPA How to get the number of parameter markers used in Criteria API?

How do I get the number of parameter markers used in Criteria API? I am using Criteria API to create SQL statement with IN keyword that have many parameter markers. CriteriaBuilder cb = ... ... CriteriaBuilder.In in = cb.in(...); ... for…
0
votes
1 answer

How to create a JPA Predicate to select entities of type B where A.someMethod(B) == true?

I'm new to the Criteria API and I'm trying to create a query that counts the entities of type B where A.someMethod(B) == true. My entities: @Entity public class A { //Polymorphic function public boolean someMethod(B b) { // Processes…
Marcel
  • 5
  • 4