Java Persistence Query Language
Үлгі:Infobox programming language Java Persistence Query Language (JPQL) объектке бағытталған платформаға байланысы жоқ мәліметтер базасының тілі.
Мысалдар
<source lang="java"> @Entity public class Publisher {
@Id private Integer id; private String name; private String address;
@OneToMany(mappedBy = "publisher") private List<Book> books;
} </source>
Then a simple query to retrieve the list of all authors, ordered alphabetically, would be:
<source lang="sql"> SELECT a FROM Author a ORDER BY a.firstName, a.lastName </source>
To retrieve the list of authors that have ever been published by XYZ Press:
<source lang="sql"> SELECT DISTINCT a FROM Author a INNER JOIN a.books b WHERE b.publisher.name = 'XYZ Press' </source>
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:
<source lang="java"> import javax.persistence.EntityManager; import javax.persistence.Query; import org.apache.commons.lang.StringUtils;
...
@SuppressWarnings("unchecked") public List<Author> getAuthorsByLastName(String lastName) {
String queryString = "SELECT a FROM Author a " + "WHERE :lastName IS NULL OR LOWER(a.lastName) = :lastName"; Query query = getEntityManager().createQuery(queryString); query.setParameter("lastName", StringUtils.lowerCase(lastName)); return query.getResultList();
} </source>
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.