41
Professional Open Source™ © JBoss, Inc. 2003, 2004. 1 07/17/04 JPA QL

15 jpaql

Embed Size (px)

Citation preview

Page 1: 15 jpaql

Professional Open Source™

© JBoss, Inc. 2003, 2004. 1

07/17/04

JPA QL

Page 2: 15 jpaql

© JBoss, Inc. 2003, 2004. 2

Professional Open Source™

Query API

The EntityManager interface methods for creating query instances, the Query interface methods that define and execute the query, and JPQL are referred to as the query API.

Page 3: 15 jpaql

© JBoss, Inc. 2003, 2004. 3

Professional Open Source™

Defining named queries

You must create a named (or static) query before you can use it. It is defined either in the entity using annotations, or in the XML file defining O/R mapping metadata.

Page 4: 15 jpaql

© JBoss, Inc. 2003, 2004. 4

Professional Open Source™

Creating a query instance

The EntityManager interface provides several methods to create queries using either JPQL or native SQL statements.

Page 5: 15 jpaql

© JBoss, Inc. 2003, 2004. 5

Professional Open Source™

Creating a named query instance

Page 6: 15 jpaql

© JBoss, Inc. 2003, 2004. 6

Professional Open Source™

Creating a dynamic query instance

Page 7: 15 jpaql

© JBoss, Inc. 2003, 2004. 7

Professional Open Source™

Setting parameters for a query

Setting Named parameters

Setting Positional Parameters

Page 8: 15 jpaql

© JBoss, Inc. 2003, 2004. 8

Professional Open Source™

Retrieving a single entity

If your query retrieves multiple instances of Category entities with the same name, it will throw NonUniqueResultException. The persistence provider will throw NoResultException when your query does notretrieve any result.

These exceptions will not roll back the active transactions.

Retrieving an entity using getSingleResult does not require an active transaction.However, if no transactions are available, the retrieved entity will be detached after retrieval.

Page 9: 15 jpaql

© JBoss, Inc. 2003, 2004. 9

Professional Open Source™

Retrieving a collection of entities

If getResultList does not retrieve any results for a query, it returns an empty list. No exceptions are thrown.

Retrieving a collection does not require anactive transaction, and if one isn’t available, the retrieved entities will be detached after retrieval.

Page 10: 15 jpaql

© JBoss, Inc. 2003, 2004. 10

Professional Open Source™

Paginating through a result list

Page 11: 15 jpaql

© JBoss, Inc. 2003, 2004. 11

Professional Open Source™

Specifying query hints

A query hint is a tip that is used by the persistence provider while executing queries or retrieving entities.

For example, a hint can be a directive to the persistence provider whether to use a cache while executing a query.

Page 12: 15 jpaql

© JBoss, Inc. 2003, 2004. 12

Professional Open Source™

Specifying Query Hints in a NamedQuery

Page 13: 15 jpaql

© JBoss, Inc. 2003, 2004. 13

Professional Open Source™

Introduction to JPQL

Page 14: 15 jpaql

© JBoss, Inc. 2003, 2004. 14

Professional Open Source™

Hibernate provides HSQL, while JDO-compliant providers such as BEA’s Kodo support JDO QL to query entities.

JPQL is an extension of EJB QL, the query language of EJB 2.

Page 15: 15 jpaql

© JBoss, Inc. 2003, 2004. 15

Professional Open Source™

How is JPQL Different from SQL?

JPQL operates on classes and objects (entities) in the Java space. SQL operates on tables, columns, and rows in the database space.

While JPQL and SQL look similar to us, they operate in two very different worlds.

Page 16: 15 jpaql

© JBoss, Inc. 2003, 2004. 16

Professional Open Source™

The JPQL Query Parser or Processor Engine of a persistence provider, as shown in below figure, translates the JPQL query into native SQL for the database being used by the persistence provider.

Page 17: 15 jpaql

© JBoss, Inc. 2003, 2004. 17

Professional Open Source™

Defining and using SELECT

Page 18: 15 jpaql

© JBoss, Inc. 2003, 2004. 18

Professional Open Source™

Defining UPDATE and DELETE

Page 19: 15 jpaql

© JBoss, Inc. 2003, 2004. 19

Professional Open Source™

Identifying the query domain: naming an entity

Page 20: 15 jpaql

© JBoss, Inc. 2003, 2004. 20

Professional Open Source™

What is a path expression?

Expressions such as c.categoryName and c.categoryId are known as path expressions

A path expression is an identifier variable followed by the navigation operator (.), and a persistence or association field.

An association field can contain either a single-value object or a collection. The association fields that represent one-to-many and many-to-many associations are collections of types, and such a path expression is a collection-value path expression.

Here c.items is collection-value path expression

Page 21: 15 jpaql

© JBoss, Inc. 2003, 2004. 21

Professional Open Source™

If the association is either many-to-one or one-to-one, then the association fields are of a specific object type, and those types are known as single value path expressions

For example, c.items.user.firstName c.items.user.contactDetails.email

Page 22: 15 jpaql

© JBoss, Inc. 2003, 2004. 22

Professional Open Source™

Conditional expressions and operators

A condition in the WHERE clause that filters results from a query is known as a conditional expression.

Operators supported by JPQL :

Page 23: 15 jpaql

© JBoss, Inc. 2003, 2004. 23

Professional Open Source™

Using a range with BETWEEN

Page 24: 15 jpaql

© JBoss, Inc. 2003, 2004. 24

Professional Open Source™

Using the IN operator

Page 25: 15 jpaql

© JBoss, Inc. 2003, 2004. 25

Professional Open Source™

Using the LIKE operator

Page 26: 15 jpaql

© JBoss, Inc. 2003, 2004. 26

Professional Open Source™

Dealing with null values and empty collections

You can use the IS NULL or IS NOT NULL operator to check whether a single-value path expression contains null or not null values.

For Example, WHERE c.parentCategory IS NOT NULL

You cannot use the IS NULL expression to compare a path expression that is of type collection. You have to use

IS [NOT] EMPTY

For Example, SELECT c FROM Category c WHERE c.items IS EMPTY

Page 27: 15 jpaql

© JBoss, Inc. 2003, 2004. 27

Professional Open Source™

Checking for the existence of an entity in a collection

Page 28: 15 jpaql

© JBoss, Inc. 2003, 2004. 28

Professional Open Source™

String functions

Page 29: 15 jpaql

© JBoss, Inc. 2003, 2004. 29

Professional Open Source™

Arithmetic functions

Page 30: 15 jpaql

© JBoss, Inc. 2003, 2004. 30

Professional Open Source™

JPQL temporal functions

Page 31: 15 jpaql

© JBoss, Inc. 2003, 2004. 31

Professional Open Source™

Aggregate functions

Page 32: 15 jpaql

© JBoss, Inc. 2003, 2004. 32

Professional Open Source™

Grouping with GROUP BY and HAVING

Page 33: 15 jpaql

© JBoss, Inc. 2003, 2004. 33

Professional Open Source™

Ordering the query result

Page 34: 15 jpaql

© JBoss, Inc. 2003, 2004. 34

Professional Open Source™

Using subqueries

Page 35: 15 jpaql

© JBoss, Inc. 2003, 2004. 35

Professional Open Source™

Subqueries using ANY,ALL,SOME

Page 36: 15 jpaql

© JBoss, Inc. 2003, 2004. 36

Professional Open Source™

Theta-joins

Page 37: 15 jpaql

© JBoss, Inc. 2003, 2004. 37

Professional Open Source™

Inner Join

SELECT u FROM User u INNER JOIN u.Category c WHERE u.userId LIKE ?1

The INNER clause is optional.

Remember that when you use the JOIN operator by itself, an inner join is always performed

Page 38: 15 jpaql

© JBoss, Inc. 2003, 2004. 38

Professional Open Source™

Outer joins

Page 39: 15 jpaql

© JBoss, Inc. 2003, 2004. 39

Professional Open Source™

Fetch joins

Page 40: 15 jpaql

© JBoss, Inc. 2003, 2004. 40

Professional Open Source™

Native SQL queries

Using dynamic queries with native SQL

Page 41: 15 jpaql

© JBoss, Inc. 2003, 2004. 41

Professional Open Source™

Using a named native SQL query