Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Jakarta Persistence Query Language

From Wikipedia, the free encyclopedia
(Redirected fromJava Persistence Query Language)
This article includes alist of references,related reading, orexternal links,but its sources remain unclear because it lacksinline citations. Please helpimprove this article byintroducing more precise citations.(June 2010) (Learn how and when to remove this message)
Jakarta Persistence Query Language
OSCross-platform
Websiteeclipse-ee4j.github.io/jakartaee-tutorial/#the-jakarta-persistence-query-language
Influenced by
SQL,Hibernate

TheJakarta Persistence Query Language (JPQL; formerly Java Persistence Query Language) is a platform-independentobject-orientedquery language[1]: 284, §12  defined as part of theJakarta Persistence (JPA; formerly Java Persistence API) specification.

JPQL is used to make queries against entities stored in a relational database. It is heavily inspired bySQL, and its queries resemble SQL queries in syntax,[1]: 17, §1.3  but operate against JPA entity objects rather than directly with database tables.[1]: 26, §2.2.3 

In addition to retrieving objects (SELECT queries), JPQL supports set basedUPDATE andDELETE queries.

Examples

[edit]

Example JPA Classes, getters and setters omitted for simplicity.

@EntitypublicclassAuthor{@IdprivateIntegerid;privateStringfirstName;privateStringlastName;@ManyToManyprivateList<Book>books;}@EntitypublicclassBook{@IdprivateIntegerid;privateStringtitle;privateStringisbn;@ManyToOneprivatePublisherpublisher;@ManyToManyprivateList<Author>authors;}@EntitypublicclassPublisher{@IdprivateIntegerid;privateStringname;privateStringaddress;@OneToMany(mappedBy="publisher")privateList<Book>books;}

Then a simple query to retrieve the list of all authors, ordered alphabetically, would be:

SELECTaFROMAuthoraORDERBYa.firstName,a.lastName

To retrieve the list of authors that have ever been published by XYZ Press:

SELECTDISTINCTaFROMAuthoraINNERJOINa.booksbWHEREb.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:

importjavax.persistence.EntityManager;importjavax.persistence.TypedQuery;...publicList<Author>getAuthorsByLastName(StringlastName){StringqueryString="SELECT a FROM Author a "+"WHERE a.lastName IS NULL OR LOWER(a.lastName) = LOWER(:lastName)";TypedQuery<Author>query=getEntityManager().createQuery(queryString,Author.class);query.setParameter("lastName",lastName);returnquery.getResultList();}

Hibernate Query Language

[edit]

JPQL is based on theHibernate Query Language (HQL), an earlier non-standard query language included in theHibernateobject-relational mapping library.

Hibernate and the HQL were created before the JPA specification.As of Hibernate 3 JPQL is a subset of HQL.

Citations

[edit]
  1. ^abcBauer, King & Gregory 2016.

References

[edit]

See also

[edit]

External links

[edit]
Retrieved from "https://en.wikipedia.org/w/index.php?title=Jakarta_Persistence_Query_Language&oldid=1303149289"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp