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
10
votes
3 answers

Complex Search Queries JPA

In my Wicket+JPA/Hibernate+Spring project, much of the functionality is based around the Inbox page where, using many filtering options (not all of them have to be used), users can restrict the set of objects they want to work with. I was wondering…
John Manak
  • 13,328
  • 29
  • 78
  • 119
10
votes
3 answers

JPA Criteria multiselect with fetch

I have following model: @Entity @Table(name = "SAMPLE_TABLE") @Audited public class SampleModel implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID") private Long id; …
Gazeciarz
  • 516
  • 1
  • 8
  • 22
10
votes
3 answers

CriteriaBuilder join two tables with a custom condition

I want to write this SQL query SELECT * FROM A LEFT OUTER JOIN B ON A.IDRESOURCE=B.IDRESOURCE AND B.IDLANGUAGE=22; with the JPA Criteria Builder. I wrote the first part of the join simply with: CriteriaQuery searchQuery =…
gvdm
  • 3,006
  • 5
  • 35
  • 73
10
votes
1 answer

How do I construct a inverse join for a unidirectional relationship?

I guess this is easy, but I can't figure it out. If I have entities Foo and Bar: @Entity class Foo{ @OneToMany List bars; } @Entity class Bar{ @Column int data; } and if I want to create a Join…
10
votes
1 answer

compound predicate in JPA Criteria Query - Use both and, or methods

Requirement: There are some initial conditions.lets say P1 is the predicate. There is a date field in database table which could be null. If it is null, I can proceed with initial predicate itself. If it is not null, the value of that field has to…
tortoise
  • 613
  • 1
  • 7
  • 19
10
votes
2 answers

JPA Criteria: Convert int to String then select from substring of resulting String

I have a String as parameter (which is in fact a valueOf(an Integer) and want to compare it to a substring of a int value in DB. Here is my code: ClinicPatients clp = null; // Get the criteria builder instance from entity manager final…
jon
  • 910
  • 3
  • 12
  • 34
10
votes
1 answer

Criteria Api: Create null value with CriteriaBuilder

Is there a better way to create a SQL null value with CriteriaBuilder than criteriaBuilder.quot(criteriaBuilder.literal(0), criteriaBuilder.literal(0)) ?
mazatwork
  • 1,275
  • 1
  • 13
  • 20
10
votes
4 answers

More usable alternative to Criteria API

Criteria has some advantages over using JPQL or raw SQL, as described in this answer: type safety; refactoring friendliness; less dependence on strings (but there still is some). And one very big disadvantage: they are less readable and simply ugly.…
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
9
votes
4 answers

Union All and Sum with JPA CriteriaBuilder

I am trying to convert a native SQL query to use the Criteria API in JPA 2.0. I have found a lot of Criteria API examples on Google, but I am having a really hard time putting all of the pieces together. I'm hoping that a more experienced person…
user1148956
  • 303
  • 1
  • 3
  • 9
9
votes
3 answers

jpa lazy fetch entities over multiple levels with criteria api

I am using JPA2 with it's Criteria API to select my entities from the database. The implementation is OpenJPA on WebSphere Application Server. All my entities are modeled with Fetchtype=Lazy. I select an entity with some criteria from the database…
Steven Rudolf
  • 275
  • 1
  • 8
  • 17
9
votes
2 answers

How to debug JPA CriteriaBuilder queries

How can I debug a query built with JPA 2.0 CriteriaBuilder? Is there a way to print out the query that is being executed? I am developing a web application using NetBeans, MySql, GlassFish. I would avoid starting MySql in debug mode, because it is…
perissf
  • 15,979
  • 14
  • 80
  • 117
9
votes
2 answers

Grails Criteria projections on joined table

I have an issue with grails criteria builder, I want to do a projection on a column that is on a table that is in one-to-many relation to parent table example: Car.createCriteria() { projections { property('name') …
csviri
  • 1,159
  • 3
  • 16
  • 31
9
votes
4 answers

How to use key of MAP in Criteria Query?

I have a Bean like this Class TestA { Map testBMap; } Class TestB { String data; ... } I want to fetch the TestA data along with the map testBMap where key ='test1'. How can i do this using Hibernate.
kandarp
  • 4,979
  • 11
  • 34
  • 43
9
votes
2 answers

JPA differences between Join and JoinSet

As the title say, I want to know the differences among these two methods. Specifically I want to know the difference between: join(String arg) AND joinSet(String arg) As I can use join(String arg) even if the attribute is a Set, but not the…
Gaetano Piazzolla
  • 1,388
  • 1
  • 16
  • 31
9
votes
3 answers

Creating queries using Criteria API (JPA 2.0)

I'm trying to create a query with the Criteria API from JPA 2.0, but I can't make it work. The problem is with the "between" conditional method. I read some documentation to know how I have to do it, but since I'm discovering JPA, I don't understand…
Pym
  • 1,018
  • 3
  • 13
  • 17