Jakarta Persistence Query Language
{{inline|date=June 2010}}
{{Infobox programming language
| name = Jakarta Persistence Query Language
| logo =
| paradigm =
| year =
| designer =
| developer =
| latest_release_version =
| latest_release_date =
| latest_test_version =
| latest_test_date =
| typing =
| implementations =
| dialects =
| influenced_by = SQL, Hibernate
| influenced =
| operating_system = Cross-platform
| license =
| website =
}}
The Jakarta Persistence Query Language (JPQL; formerly Java Persistence Query Language) is a platform-independent object-oriented query language{{sfn|Bauer|King|Gregory|2016}}{{rp|loc=§12|p=284}} defined as part of the Jakarta Persistence (JPA; formerly Java Persistence API) specification.
JPQL is used to make queries against entities stored in a relational database. It is heavily inspired by SQL, and its queries resemble SQL queries in syntax,{{sfn|Bauer|King|Gregory|2016}}{{rp|loc=§1.3|p=17}} but operate against JPA entity objects rather than directly with database tables.{{sfn|Bauer|King|Gregory|2016}}{{rp|loc=§2.2.3|p=26}}
In addition to retrieving objects (SELECT
queries), JPQL supports set based UPDATE
and DELETE
queries.
Examples
Example JPA Classes, getters and setters omitted for simplicity.
@Entity
public class Author {
@Id
private Integer id;
private String firstName;
private String lastName;
@ManyToMany
private List
}
@Entity
public class Book {
@Id
private Integer id;
private String title;
private String isbn;
@ManyToOne
private Publisher publisher;
@ManyToMany
private List
}
@Entity
public class Publisher {
@Id
private Integer id;
private String name;
private String address;
@OneToMany(mappedBy = "publisher")
private List
}
Then a simple query to retrieve the list of all authors, ordered alphabetically, would be:
SELECT a FROM Author a ORDER BY a.firstName, a.lastName
To retrieve the list of authors that have ever been published by XYZ Press:
SELECT DISTINCT a FROM Author a INNER JOIN a.books b WHERE b.publisher.name = 'XYZ Press'
JPQL supports named parameters, which begin with the colon (:
). We could write a function returning a list of authors with the given last name as follows:
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
...
public List
String queryString = "SELECT a FROM Author a " +
"WHERE a.lastName IS NULL OR LOWER(a.lastName) = LOWER(:lastName)";
TypedQuery
query.setParameter("lastName", lastName);
return query.getResultList();
}
Hibernate Query Language
JPQL is based on the Hibernate Query Language (HQL), an earlier non-standard query language included in the Hibernate object-relational mapping library.
Hibernate and the HQL were created before the JPA specification.
As of Hibernate 3 JPQL is a subset of HQL.
Citations
{{Reflist}}
References
- {{citation
|last1 = Bauer
|first1 = Christian
|last2 = King
|first2 = Gavin
|last3 = Gregory
|first3 = Gary
| year = 2016
| title = Java Persistence with Hibernate
| edition = Second
| publisher = Manning Publications
| isbn = 978-1617290459
| url =
}}
See also
External links
- {{official website}}
- [https://eclipse-ee4j.github.io/jakartaee-tutorial/#full-query-language-syntax Full Query Language Syntax from The Jakarta EE 8 Tutorial]
- [https://www.objectdb.com/java/jpa/query JPA Queries and JPQL - a chapter of the ObjectDB Manual]
- [https://torpedoquery.org/ Type safe Hibernate (HQL) query engine - TorpedoQuery]
{{Jakarta Persistence}}