Java Persistence Query Language
Encyclopedia
|
|
|
|
![]()
Java Persistence Query Language
The Java Persistence Query Language (JPQL) is a platform-independent object-oriented query language defined as part of the 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, but operate against JPA entity objects rather than directly with database tables. In addition to retrieving objects (
ExamplesSuppose we have JPA entity classes defined like this (getter and setter methods omitted for simplicity):
@Entity
public class Author {
@Id
private Integer id;
private String firstName;
private String lastName;
@ManyToMany
private List<Book> books;
}
@Entity
public class Book {
@Id
private Integer id;
private String title;
private String isbn;
@ManyToOne
private Publisher publisher;
@ManyToMany
private List<Author> authors;
}
@Entity
public class Publisher {
@Id
private Integer id;
private String name;
private String address;
@OneToMany(mappedBy = "publisher")
private List<Book> books;
}
Then a simple query to retrieve the list of all authors, ordered alphabetically, would be: To retrieve the list of authors that have ever been published by XYZ Press: JPQL supports named parameters, which begin with the colon (
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();
}
Hibernate Query LanguageJPQL 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. See also
External links
Source: Wikipedia | The above article is available under the GNU FDL. | Edit this article
|
|
top
©2008-2009 TutorGig.com. All Rights Reserved. Privacy Statement