JPA QL

Now that you have a basic understanding of how to work with Query objects, you can learn what features are available to you for creating your own JPA QL queries. JPA QL is expressed in terms of the abstract persistence schema of an entity: its abstract schema name, basic properties, and relationship properties. JPA QL uses the abstract schema names to identify beans, the basic properties to specify values, and the relationship properties to navigate across relationships.

To discuss JPA QL, we will use the relationships defined in Chapter 11.

Abstract Schema Names

The abstract schema name can be defined by metadata, or it can default to a specific value. It defaults to the unqualified name of the entity bean class if the name() attribute is not specified when declaring the @Entity annotation.

In the following example, the @Entity.name() attribute is not specified on the Employee bean class, so Employee is used to reference the entity within JPA QL calls:

@Entity
public class Employee {...}

entityManager.createQuery("SELECT e FROM Employee AS e");

In the following example, since the @Entity.name() attribute is defined, you would reference Employee entities in JPA QL as Emp:

@Entity(name="Emp")
public class Employee {...}

entityManager.createQuery("SELECT e FROM Emp AS e");

Simple Queries

The simplest JPA QL statement has no WHERE clause and only one abstract schema type. For example, you could define a query method to select all Employee beans:

SELECT OBJECT( e ) FROM Employee AS e

Get Enterprise JavaBeans 3.1, 6th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.