Jakarta Persistence Query Language: Difference between revisions

Content deleted Content added
Examples: Changed example query to use TypedQuery for improved type safety. If we really want an example using normal Query, I'd strongly suggest making another example with an update or delete (using createNativeQuery if necessary) to avoid the need for a result.
Tags: Mobile edit Mobile web edit
m Disambiguating links to Object-orientation (link changed to Object-oriented programming) using DisamAssist.
 
(15 intermediate revisions by 8 users not shown)
Line 1:
{{norefinline|date=June 2010}}
{{Infobox programming language
| name = JavaJakarta Persistence Query Language
| logo =
| paradigm =
Line 11:
| latest_test_version =
| latest_test_date =
| turing-complete = No
| typing =
| implementations =
| dialects =
| influenced_by = [[SQL]], [[Hibernate_Hibernate (Java)#Hibernate_Query_LanguageHibernate Query Language (HQL)|Hibernate]]
| influenced =
| operating_system = [[Cross-platform]]
Line 21 ⟶ 20:
| website =
}}
The '''JavaJakarta Persistence Query Language''' ('''JPQL'''; formerly Java Persistence Query Language) is a platform-independent [[Object-oriented programming|object-oriented]] [[query language]]{{sfn|Bauer|King|Gregory|2016}}{{rp|loc=§12|p=284}} defined as part of the [[JavaJakarta Persistence API]] (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 (<code>SELECT</code> queries), JPQL supports set based <code>UPDATE</code> and <code>DELETE</code> queries.
Line 31 ⟶ 30:
Example JPA Classes, getters and setters omitted for simplicity.
 
<sourcesyntaxhighlight lang="java">
@Entity
public class Author {
Line 67 ⟶ 66:
private List<Book> books;
}
</syntaxhighlight>
</source>
 
Then a simple query to retrieve the list of all authors, ordered alphabetically, would be:
 
<sourcesyntaxhighlight lang="sql">
SELECT a FROM Author a ORDER BY a.firstName, a.lastName
</syntaxhighlight>
</source>
 
To retrieve the list of authors that have ever been published by XYZ Press:
 
<sourcesyntaxhighlight lang="sql">
SELECT DISTINCT a FROM Author a INNER JOIN a.books b WHERE b.publisher.name = 'XYZ Press'
</syntaxhighlight>
</source>
 
JPQL supports named parameters, which begin with the colon (<code>:</code>). We could write a function returning a list of authors with the given last name as follows:
 
<sourcesyntaxhighlight lang="java">
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
Line 93 ⟶ 92:
"WHERE a.lastName IS NULL OR LOWER(a.lastName) = LOWER(:lastName)";
 
TypedQuery<Author> query = getEntityManager().createQuery(queryString, Author.class());
query.setParameter("lastName", lastName);
return query.getResultList();
}
</syntaxhighlight>
</source>
 
==Hibernate Query Language==
 
JPQL is based on the [[Hibernate_Hibernate (Java)#Hibernate_Query_Language_Hibernate Query Language (HQL)|Hibernate Query Language]] (HQL), an earlier non-standard query language included in the [[Hibernate (Java)|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==
* [[Object-relational mapping]]
* [[Hibernate (Javaframework)]]
* [[JavaJakarta Persistence API]]
* [[ActiveJPA]]
 
== External links ==
* {{official website}}
** [httphttps://docseclipse-ee4j.oraclegithub.com/javaee/6io/jakartaee-tutorial/doc/bnbuf.html#full-query-language-syntax Full Query Language Syntax from The JavaJakarta EE 68 Tutorial]
* [httphttps://www.objectdb.com/java/jpa/query JPA Queries and JPQL - a chapter of the ObjectDB Manual]
* [httphttps://torpedoquery.org/ Type safe Hibernate (HQL) query engine - TorpedoQuery]
 
{{JavaJakarta Persistence API}}
 
[[Category:Query languages]]