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:
{{
{{Infobox programming language
| name =
| logo =
| paradigm =
Line 11:
| latest_test_version =
| latest_test_date =
| typing =
| implementations =
| dialects =
| influenced_by = [[SQL]], [[
| influenced =
| operating_system = [[Cross-platform]]
Line 21 ⟶ 20:
| website =
}}
The '''
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.
<
@Entity
public class Author {
Line 67 ⟶ 66:
private List<Book> books;
}
</syntaxhighlight>
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
</syntaxhighlight>
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'
</syntaxhighlight>
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:
<
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>
==Hibernate Query Language==
JPQL is based on the [[
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 (
* [[
== External links ==
* {{official website}}
** [
* [
* [
{{
[[Category:Query languages]]
|