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

JPA Criteria Query, how to join entities - Hibernate-

I have made a JPA project (using hibernate). It's a bit more complex than this, but here are 2 classes: @Entity @Table(name= "Persons") public class Person { @Id @Column(nullable= false) @GeneratedValue(strategy = GenerationType.AUTO) …
Jan Ossowski
  • 328
  • 1
  • 2
  • 18
0
votes
1 answer

Hibernate Criteria API: count and group by with result in Map

The Car entity is mapped to a database table with 2 columns: ID and Color. CarDao has the following method: Map countByColor(); If we have 3 red cars and 2 blue cars in the database table, the method returns a map with 2 keys (red…
Francois
  • 2,289
  • 4
  • 20
  • 21
0
votes
2 answers

What is purpose of metamodel class in JPA2.0?

I was recently reading about the Criteria API in JPA2.0 specification, where they introduced the concept of having metamodel class (see here). I understand how to use it, but what I do not understand is why was there a need to create this new…
Siddharth
  • 2,046
  • 5
  • 26
  • 41
0
votes
1 answer

JPA Criteria construct with non-entity parameter

I want to create a list of complex DTO objects with data from several Entities and one non-Entity-parameter. Let's say my DTO class has constructor: public MyDto(String entityField, String someString) {...} and I would like to use the…
golinko
  • 327
  • 2
  • 3
  • 14
0
votes
0 answers

SubQuery can't group by in Criteria API

Hi I have a problem using groupBy with subQuery DB result _______________________ | id | departure | |______|______________| | 99 | 00:54 | |______|______________| | 100 | 00:54 | |______|______________| I need | id | …
luprogrammer
  • 155
  • 1
  • 3
  • 12
0
votes
2 answers

Set value in mongodb array element

I have a document like this: { "_id" : "1", "messages" : [ { "_id" : "second", "conversationId" : "1", "timestamp" : ISODate("2015-03-01T20:16:30.991Z"), "text" : "cool text", …
Tymur Yarosh
  • 553
  • 1
  • 6
  • 19
0
votes
1 answer

JPA CriteriaBuilder with junction table

How can the sql expression below be expressed using CriteriaBuilder? select * from Ref where prac_id = (select prac_id from loc l join staff_loc sl where sl.loc = l.id and sl.pracstaff_id = 123) Model Classes @Entity public class Ref { private…
meja
  • 479
  • 1
  • 6
  • 11
0
votes
1 answer

Load collections eagerly in NHibernate using Criteria API

I have an entity A which HasMany entities B and entities C. All entities A, B and C have some references x,y and z which should be loaded eagerly. I want to read from the database all entities A, and load the collections of B and C eagerly using…
0
votes
1 answer

JPA Criteria api using IN expression with join

i have entity Request that have @ManyToMany Set regions, and Region entity have field region of type RegionEnum of enum type with constants of regions. I need to create criteria to get requests, where its regions are in collection of…
Vadim
  • 105
  • 1
  • 9
0
votes
1 answer

JPA Criteria Query with multiple IN on different levels

I have these (simplified) entities: @Entity public class Project { @ManyToOne private Type type; } @Entity public class Type { @ManyToOne private Category category; } @Entity public class Category { } No I want to query all projects…
Thor
  • 6,607
  • 13
  • 62
  • 96
0
votes
1 answer

How select column at rune time using JPA (JPQL or Criteria API)?

I've searched here on SO and the web but couldn't find an answer to this problem. How do I write the following MySQL query using JPQL or JPA's Criteria API? public String getLanguage(String language) { String query = "SELECT i18n." + language +…
Borche
  • 33
  • 7
0
votes
1 answer

OpenJPA join query does not return any data

I have entities structure for keeping building, rooms and floors like this: Building -- one to many -- Floor -- one to many Room I'm looking at Criteria API and my goal is to get all rooms first associated with same floor and than to find all rooms…
Anton
  • 1,409
  • 1
  • 19
  • 37
0
votes
1 answer

JPA basic type ElementCollection computed value in query

Consider the following Entity : @Entity public class Parent{ @Column private String name; @ElementCollection @CollectionTable(name="REF_TABLE",joinColumns=@JoinColumn(name="REF_COLUMN_ID")) @Column(name="REF_COLUMN_VALUE") public Set
Alexander.Furer
  • 1,817
  • 1
  • 16
  • 24
0
votes
1 answer

The query has not been defined correctly, the expression builder is missing

Since we upgraded from Glassfish 3 to Glassfish 4.1 we've been getting this error. I'm trying to get data from a database but it fails most of the time. Yes, most of the time, not always. When I run it and it fails, it gives me this…
Erates
  • 646
  • 1
  • 9
  • 24
0
votes
2 answers

Set timeout on a TypedQuery with JPA2

I would like to set a timeout on a javax.persistence.TypedQuery. I've found this easy method : TypedQuery query = ... ; query.setHint("javax.persistence.query.timeout", 1000); query.getReturnList(); But it seems that does not work, it's just…
Gaetano Piazzolla
  • 1,388
  • 1
  • 16
  • 31