Ch08 query language - skatscher/pro_jpa2_book GitHub Wiki

Introduction

  • JPQL is not SQL: JPQL provides a way to express queries in terms of entities and their relationships, operating on the persistent state of the entity as defined in the object model, not in the physical database model
  • JPQL advantages:
    • portable: can be translated into SQL dialects
    • queries can be written against the domain model of persistent entities, without any need to know exactly how those entities are mapped to the database

Terminology

  • Queries fall into one of the 4 catagories:
    • select: to retrieve persistent state
    • aggregate: to produce summary data
    • update: modify sets of entities
    • delete: remove sets of entities
  • Report queries: select and aggregate queries
  • Abstract persistence schema: set of entities and embeddables, the overall domain model
  • Abstract schema name: unqualified class name, which is used by default as name in context of the query
  • Abstract schema type: class or primitive type to implement persistence property as field or JavaBean (e.g. String is the abstract schema type for field name of Employee)
  • state fields: simple persistent properties without relationship mappings
  • association fields: persistent properties that are also relationships
  • Queries can be statically or dynamically defined
  • Queries are not case-sensitive except in two cases: entity names and property names

Select Queries

  • overall form
SELECT <select_expression>
FROM <from_clause>
[WHERE <conditional_expression>]
[ORDER BY <order_by_clause>]
  • Example:
SELECT e
FROM Employee e
  • Employee is an entity
  • e is an identification variable, which is the key by which the entity is referred to in the rest of the select statement. The use of the identification variable is mandatory.
  • every JPQL is translatable to SQL