JPQL is a powerful query language that allows you to define database queries based on your It is used in all other parts of the query to reference this entity. The Java Persistence Query Language (JPQL) is the query language defined by JPA. JPQL can be used in a NamedQuery (through annotations or XML) or in. These extensions, referred to as the EclipseLink Query Language (EQL), provide access to “Query Language” in the JPA Specification.
|Published (Last):||3 August 2008|
|PDF File Size:||4.76 Mb|
|ePub File Size:||14.11 Mb|
|Price:||Free* [*Free Regsitration Required]|
The Java Persistence query language JPQL is used to define searches against persistent entities independent of the mechanism used to store those entities. As such, JPQL is “portable”, and not constrained to any particular data store. The Java Persistence query language is an extension of the Enterprise JavaBeans query language, EJB QLadding operations such as bulk deletes and updates, join operations, aggregates, projections, and subqueries.
Furthermore, JPQL queries can be declared statically in metadata, or can be dynamically built in code. This chapter provides the full definition of the lannguage. Note Much of this section is paraphrased or taken directly from Chapter 4 of the JSR specification.
JPA – JPQL
This chapter refers to all such statements as “queries”. Where it is important to distinguish among statement types, the specific statement type is referenced. In BNF syntax, a query language statement is defined as: A select statement is ,anguage string which consists of the following clauses: Update and delete statements provide bulk operations over sets of entities.
In BNF syntax, these operations are defined as: The Java Persistence query language is a typed language, and every expression has a type. The type of an expression is derived from the structure of the expression, the abstract schema types of the identification variable declarations, the types to which the persistent fields and relationships evaluate, and the types of literals.
The abstract schema type of an entity is derived from the entity class and the metadata information provided by Java language annotations or in the XML descriptor. Informally, the abstract schema type of an entity can be rfeerence as follows: For every persistent field or get accessor method for a persistent property of the entity class, there is a field “state-field” whose abstract schema type corresponds to that of the field or the result type of languagd accessor method.
For every persistent relationship field or get accessor method for a persistent relationship property of the entity class, there is a field “association-field” whose type is the abstract schema type of the related entity or, if the relationship is a one-to-many or many-to-many, a collection of such. Abstract schema types are specific to the query language data model. The persistence provider is not required to implement or otherwise materialize an abstract schema type.
The domain of a query consists of the abstract schema types of all entities that are defined in the same persistence unit. The domain of a query may refersnce restricted by the navigability of the relationships of the entity on which it is based. The association-fields of an entity’s abstract schema type determine navigability.
Using the association-fields and their values, a query can select related entities and use their abstract schema types in the query.
Entities are designated in query strings by their entity names. The entity name is defined by the name element of the Entity annotation or the entity-name XML descriptor elementand defaults to the upql name of the entity class. Entity names are scoped within the persistence unit and referencee be unique within the persistence unit. This example assumes that the application developer provides several entity classes representing magazines, publishers, authors, and articles.
The abstract schema types for these entities are MagazinePublisherAuthorand Article. The entity Publisher has a one-to-many relationships with Magazine. There is also a one-to-many relationship between Magazine and Article. The entity Article is related to Author in a one-to-one relationship. Queries to select magazines can be defined by navigating over the association-fields and state-fields defined by Magazine and Author.
A query to find all magazines that have unpublished articles is as follows: Because the same persistence unit defines the abstract persistence schemas of the related entities, the developer can also specify a query over articles that utilizes the abstract schema type for products, and hence the state-fields and association-fields of both the abstract schema types Magazine and Author.
For example, if the abstract schema type Author has a recerence named firstNamea query over articles can be specified using this state-field. Such a query might be to find all magazines that have articles authored by someone with the first name “John”.
This query is specified by using the abstract schema name Magazinewhich designates the abstract schema type over which the query ranges. The basis for the navigation is provided by the association-fields authors and product of the abstract schema types Magazine and Article respectively. The FROM clause of a query defines referrnce domain of the query by declaring identification variables. An identification variable is an identifier declared in the FROM clause of a query.
The domain of the query may be constrained by path expressions. Identification variables designate instances of a particular entity abstract schema type. The FROM clause can contain multiple identification variable declarations separated by a comma. An identifier is a character sequence of unlimited length. The character sequence must begin with a Java identifier start character, and all other characters must be Java identifier part characters.
An identifier start character is any character for which the method Character. An identifier-part character is any character for which the method Character. The following are reserved identifiers: An identification variable is a valid refetence declared in the FROM clause of a query.
All identification variables must be declared in the FROM clause. Identification variables cannot be declared in other clauses.
An identification variable must not be a reserved identifier or have the same name as any entity in the same persistence unit: Identification variables are case insensitive. An identification variable evaluates to a value of the type of the expression used in declaring the variable.
For example, consider the previous query: The association-field articles is a collection of instances of the abstract schema type Article and the identification variable art refers to an element of this collection. The type of auth is the abstract schema type of Author.
Ultimate Guide to JPQL Queries with JPA and Hibernate
An identification variable ranges over the abstract schema type of an entity. An identification variable designates an instance of an entity abstract schema type or an element of a collection of entity abstract schema type instances. Identification variables are existentially quantified in a query. An identification variable always designates a reference to a single value. It is declared in one of three ways: The identification variable declarations are evaluated from left to right in the FROM clause, and an identification variable declaration can use the result of a preceding identification variable declaration of the query string.
The syntax for declaring an identification variable as a range variable is similar to that of SQL; optionally, it uses the AS keyword.
Range variable declarations allow the developer to designate a “root” for objects which may not be reachable by navigation. In order to select values by comparing more than one instance of an entity abstract schema type, more than one identification variable ranging over the abstract schema type is needed in the FROM clause.
The following query returns magazines whose prices are greater than lnguage price of magazines published by “Adventure” publishers. This example illustrates the use of two different identification variables in the FROM clause, both of the abstract schema type Magazine. The SELECT clause of this query determines that it is the magazines with prices greater than those of “Adventure” publisher’s rfference are returned.
An identification variable followed by the navigation operator. The type of the path expression is the type computed as the result of navigation; that is, the type of the state-field or association-field to which the expression navigates. Depending on navigability, a path expression that leads to an association-field may be further composed. Path expressions can be composed from other path expressions if the original path expression evaluates to a single-valued type not a collection corresponding to an association-field.
Path-expression navigability is composed using “inner join” semantics.
That is, if the value of a non-terminal association-field in the path expression is null, the path is considered to have no value, and does not participate in the determination of refwrence result. The syntax for single-valued path expressions and collection-valued path expressions is as follows:.
Navigation to a related entity results in a value of the related entity’s abstract schema type. The evaluation of a path lanbuage terminating in a state-field results in the abstract schema type corresponding to the Java type designated by the state-field. It is syntactically illegal languagd compose a path rederence from a path expression that evaluates to a collection.
For example, if mag designates Magazinethe path expression mag. This case should produce an error when the query string is verified. To handle such a navigation, an identification variable must be declared in the FROM clause to range over the elements of the articles collection.
Another path expression must be used to navigate over each such langkage in the WHERE clause of the query, as in the following query, which returns all authors that have any articles in any magazines: This type of join typically equates to a join over a foreign key relationship in the database.
It selects those publishers with revenue of over 1 million for which at least one magazine exists: They enable the retrieval of a set of entities where matching values in the join condition may be absent. The syntax for a left outer join is: It is not permitted to specify an identification variable for the entities referenced by the right side languags the FETCH JOIN clause, and hence references to the implicitly fetched entities cannot appear elsewhere in the query.
The following query returns a set of magazines. As a side effect, the associated articles for those magazines are also retrieved, even though they are not part of the explicit query result. The persistent languae or properties of the articles that are eagerly fetched are fully initialized.
The initialization of the relationship properties of the articles that are retrieved is determined by the metadata for the Article entity class. A fetch join has the same join semantics as the corresponding inner or outer join, except that the related objects specified on the right-hand side of the join operation are not returned in the query result or otherwise referenced in the query. Hence, for example, if magazine id 1 has five articles, the above query returns five references to the magazine 1 entity.
Such a path expression represents a navigation involving the association-fields of an entity abstract schema type. Because a path expression can be based on another path expression, the navigation can use the association-fields of related entities. An identification variable of a collection member declaration is declared using a special operator, the reserved identifier IN.
The argument to the IN operator is a collection-valued path expression.