Java Reference
In-Depth Information
11.1.2. FROM clause
The FROM clause of JPQL is by far the most important clause. It defines the domain for the
query—that is, the names for the entities that will be used in the query. If your JPQL query
is to get Category entities, you specify the FROM clause as follows:
FROM Category c
Category is the domain that you want to query, and here you have specified c as an iden-
tifier of type Category .
Identifying the query domain: naming an entity
You specify the entity name defined for the entity using the @Entity annotation as the do-
main type. You can define the name for an entity using the name element of the @Entity
annotation. If you don't specify the name element, it defaults to the name of the entity
class. The name of the entity must be unique within a persistence unit. In other words,
you can't have two entities with the same name or the persistence provider will generate a
deployment error. This makes sense because the persistence provider wouldn't be able to
identify which entity domain to use if duplicate names for entities were allowed.
In the previous example, you're assuming the Category entity class that we discussed in
earlier chapters doesn't define a name. If you assume that the Category class defines an
entity name using the name element as follows
@Entity(name = "CategoryEntity")
public class Category
then you must change the FROM clause of the query as follows:
FROM CategoryEntity c
This change is required for JPQL to map the correct entity type as defined by the annota-
tion.
Search WWH ::




Custom Search