SlideShare una empresa de Scribd logo
1 de 41
Professional Open Source™




           JPA QL




© JBoss, Inc. 2003, 2004.                    07/17/04   1
Query API
                                                        Professional Open Source™


  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.




© JBoss, Inc. 2003, 2004.                                                           2
Defining named queries
                                                            Professional Open Source™


  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.




© JBoss, Inc. 2003, 2004.                                                               3
Creating a query instance
                                                           Professional Open Source™


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




© JBoss, Inc. 2003, 2004.                                                              4
Creating a named query instance
                                         Professional Open Source™




© JBoss, Inc. 2003, 2004.                                            5
Creating a dynamic query instance
                                           Professional Open Source™




© JBoss, Inc. 2003, 2004.                                              6
Setting parameters for a query
                                              Professional Open Source™


              Setting Positional Parameters




         Setting Named parameters




© JBoss, Inc. 2003, 2004.                                                 7
Retrieving a single entity
                                                              Professional Open Source™




         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 not
         retrieve 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.
© JBoss, Inc. 2003, 2004.                                                                 8
Retrieving a collection of entities
                                                                Professional Open Source™




         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 an
         active transaction, and if one isn’t available, the retrieved
         entities will be detached after retrieval.




© JBoss, Inc. 2003, 2004.                                                                   9
Paginating through a result list
                                          Professional Open Source™




© JBoss, Inc. 2003, 2004.                                             10
Specifying query hints
                                                              Professional Open Source™


  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.




© JBoss, Inc. 2003, 2004.                                                                 11
Specifying Query Hints in a NamedQuery
                                                Professional Open Source™




© JBoss, Inc. 2003, 2004.                                                   12
Professional Open Source™




            Introduction to JPQL




© JBoss, Inc. 2003, 2004.                                      13
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.




© JBoss, Inc. 2003, 2004.                                                            14
How is JPQL Different from SQL?
                                                          Professional Open Source™


  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.




© JBoss, Inc. 2003, 2004.                                                             15
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.




© JBoss, Inc. 2003, 2004.                                                             16
Defining and using SELECT
                                   Professional Open Source™




© JBoss, Inc. 2003, 2004.                                      17
Defining UPDATE and DELETE
                                    Professional Open Source™




© JBoss, Inc. 2003, 2004.                                       18
Identifying the query domain: naming an entity
                                                        Professional Open Source™




© JBoss, Inc. 2003, 2004.                                                           19
What is a path expression?
                                                             Professional Open Source™


  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

© JBoss, Inc. 2003, 2004.                                                                20
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




© JBoss, Inc. 2003, 2004.                                                                21
Conditional expressions and operators
                                                            Professional Open Source™


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

  Operators supported by JPQL :




© JBoss, Inc. 2003, 2004.                                                               22
Using a range with BETWEEN
                                    Professional Open Source™




© JBoss, Inc. 2003, 2004.                                       23
Using the IN operator
                               Professional Open Source™




© JBoss, Inc. 2003, 2004.                                  24
Using the LIKE operator
                                 Professional Open Source™




© JBoss, Inc. 2003, 2004.                                    25
Dealing with null values and empty collections
                                                        Professional Open Source™


  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


© JBoss, Inc. 2003, 2004.                                                           26
Checking for the existence of an entity in a collection
                                                                 Professional Open Source™




© JBoss, Inc. 2003, 2004.                                                                    27
String functions
                            Professional Open Source™




© JBoss, Inc. 2003, 2004.                               28
Arithmetic functions
                              Professional Open Source™




© JBoss, Inc. 2003, 2004.                                 29
JPQL temporal functions
                                   Professional Open Source™




© JBoss, Inc. 2003, 2004.                                      30
Aggregate functions
                             Professional Open Source™




© JBoss, Inc. 2003, 2004.                                31
Grouping with GROUP BY and HAVING
                                           Professional Open Source™




© JBoss, Inc. 2003, 2004.                                              32
Ordering the query result
                                   Professional Open Source™




© JBoss, Inc. 2003, 2004.                                      33
Using subqueries
                            Professional Open Source™




© JBoss, Inc. 2003, 2004.                               34
Subqueries using ANY,ALL,SOME
                                       Professional Open Source™




© JBoss, Inc. 2003, 2004.                                          35
Theta-joins
                            Professional Open Source™




© JBoss, Inc. 2003, 2004.                               36
Inner Join
                                                         Professional Open Source™


  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




© JBoss, Inc. 2003, 2004.                                                            37
Outer joins
                            Professional Open Source™




© JBoss, Inc. 2003, 2004.                               38
Fetch joins
                            Professional Open Source™




© JBoss, Inc. 2003, 2004.                               39
Native SQL queries
                                           Professional Open Source™


  Using dynamic queries with native SQL




© JBoss, Inc. 2003, 2004.                                              40
Using a named native SQL query
                                        Professional Open Source™




© JBoss, Inc. 2003, 2004.                                           41

Más contenido relacionado

La actualidad más candente

01 persistence and domain modeling
01 persistence and domain modeling01 persistence and domain modeling
01 persistence and domain modeling
thirumuru2012
 
Session 7 Tp7
Session 7 Tp7Session 7 Tp7
Session 7 Tp7
phanleson
 
3.java database connectivity
3.java database connectivity3.java database connectivity
3.java database connectivity
web360
 
Session 3 Tp3
Session 3 Tp3Session 3 Tp3
Session 3 Tp3
phanleson
 

La actualidad más candente (19)

Enrichment lecture EE Technion (part B) on the subject of VHDL-2008 (April 2012)
Enrichment lecture EE Technion (part B) on the subject of VHDL-2008 (April 2012)Enrichment lecture EE Technion (part B) on the subject of VHDL-2008 (April 2012)
Enrichment lecture EE Technion (part B) on the subject of VHDL-2008 (April 2012)
 
01 persistence and domain modeling
01 persistence and domain modeling01 persistence and domain modeling
01 persistence and domain modeling
 
Ecom lec3 16_ej_bs
Ecom lec3 16_ej_bsEcom lec3 16_ej_bs
Ecom lec3 16_ej_bs
 
Jpa basics
Jpa basicsJpa basics
Jpa basics
 
15 jpa introduction
15 jpa introduction15 jpa introduction
15 jpa introduction
 
Intershop bo
Intershop boIntershop bo
Intershop bo
 
Collections in-csharp
Collections in-csharpCollections in-csharp
Collections in-csharp
 
Session 7 Tp7
Session 7 Tp7Session 7 Tp7
Session 7 Tp7
 
Ejb3 Presentation
Ejb3 PresentationEjb3 Presentation
Ejb3 Presentation
 
Enterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business LogicEnterprise Java Beans 3 - Business Logic
Enterprise Java Beans 3 - Business Logic
 
EJB 3.0 Walkthrough (2006)
EJB 3.0 Walkthrough (2006)EJB 3.0 Walkthrough (2006)
EJB 3.0 Walkthrough (2006)
 
3.java database connectivity
3.java database connectivity3.java database connectivity
3.java database connectivity
 
EJB .
EJB .EJB .
EJB .
 
EJB3 Advance Features
EJB3 Advance FeaturesEJB3 Advance Features
EJB3 Advance Features
 
Java EE EJB Applications
Java EE EJB ApplicationsJava EE EJB Applications
Java EE EJB Applications
 
Entity beans in java
Entity beans in javaEntity beans in java
Entity beans in java
 
Session 3 Tp3
Session 3 Tp3Session 3 Tp3
Session 3 Tp3
 
Jsf+ejb 50
Jsf+ejb 50Jsf+ejb 50
Jsf+ejb 50
 
Introduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examplesIntroduction to JPA and Hibernate including examples
Introduction to JPA and Hibernate including examples
 

Destacado

JPQL/ JPA Activity 2
JPQL/ JPA Activity 2JPQL/ JPA Activity 2
JPQL/ JPA Activity 2
SFI
 
JPQL/ JPA Activity 1
JPQL/ JPA Activity 1JPQL/ JPA Activity 1
JPQL/ JPA Activity 1
SFI
 
JPQL/ JPA Activity 3
JPQL/ JPA  Activity 3JPQL/ JPA  Activity 3
JPQL/ JPA Activity 3
SFI
 
Web Services Part 2
Web Services Part 2Web Services Part 2
Web Services Part 2
patinijava
 
Patni Hibernate
Patni   HibernatePatni   Hibernate
Patni Hibernate
patinijava
 
FinelyMe-JustFit Intro
FinelyMe-JustFit IntroFinelyMe-JustFit Intro
FinelyMe-JustFit Intro
Cheng Ta Yeh
 
Web Services Part 1
Web Services Part 1Web Services Part 1
Web Services Part 1
patinijava
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transaction
patinijava
 

Destacado (20)

Working with jpa
Working with jpaWorking with jpa
Working with jpa
 
JPQL/ JPA Activity 2
JPQL/ JPA Activity 2JPQL/ JPA Activity 2
JPQL/ JPA Activity 2
 
JPQL/ JPA Activity 1
JPQL/ JPA Activity 1JPQL/ JPA Activity 1
JPQL/ JPA Activity 1
 
JPQL/ JPA Activity 3
JPQL/ JPA  Activity 3JPQL/ JPA  Activity 3
JPQL/ JPA Activity 3
 
Web Services Part 2
Web Services Part 2Web Services Part 2
Web Services Part 2
 
Ejb5
Ejb5Ejb5
Ejb5
 
Patni Hibernate
Patni   HibernatePatni   Hibernate
Patni Hibernate
 
How to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applicationsHow to bake reactive behavior into your Java EE applications
How to bake reactive behavior into your Java EE applications
 
Quickstart for continuous integration
Quickstart for continuous integrationQuickstart for continuous integration
Quickstart for continuous integration
 
Introduction to developing modern web apps
Introduction to developing modern web appsIntroduction to developing modern web apps
Introduction to developing modern web apps
 
FinelyMe-JustFit Intro
FinelyMe-JustFit IntroFinelyMe-JustFit Intro
FinelyMe-JustFit Intro
 
Continuous integration practices to improve the software quality
Continuous integration practices to improve the software qualityContinuous integration practices to improve the software quality
Continuous integration practices to improve the software quality
 
Web Services Part 1
Web Services Part 1Web Services Part 1
Web Services Part 1
 
Designing Scalable Applications
Designing Scalable ApplicationsDesigning Scalable Applications
Designing Scalable Applications
 
Spring Transaction
Spring TransactionSpring Transaction
Spring Transaction
 
Continuous testing in agile projects 2015
Continuous testing in agile projects 2015Continuous testing in agile projects 2015
Continuous testing in agile projects 2015
 
Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
JDBC - JPA - Spring Data
JDBC - JPA - Spring DataJDBC - JPA - Spring Data
JDBC - JPA - Spring Data
 
Introduction To Spring
Introduction To SpringIntroduction To Spring
Introduction To Spring
 
Java Persistence API
Java Persistence APIJava Persistence API
Java Persistence API
 

Similar a 15 jpaql

12 global fetching strategies
12 global fetching strategies12 global fetching strategies
12 global fetching strategies
thirumuru2012
 
12 hibernate int&cache
12  hibernate int&cache12  hibernate int&cache
12 hibernate int&cache
thirumuru2012
 
10 conversations new
10 conversations new10 conversations new
10 conversations new
thirumuru2012
 
11 transitive persistence and filters
11 transitive persistence and filters11 transitive persistence and filters
11 transitive persistence and filters
thirumuru2012
 
Comparing glassfish-jboss
Comparing glassfish-jbossComparing glassfish-jboss
Comparing glassfish-jboss
hung170872
 
Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6
Ray Ploski
 

Similar a 15 jpaql (20)

13 caching latest
13 caching   latest13 caching   latest
13 caching latest
 
12 global fetching strategies
12 global fetching strategies12 global fetching strategies
12 global fetching strategies
 
12 hibernate int&cache
12  hibernate int&cache12  hibernate int&cache
12 hibernate int&cache
 
10 conversations new
10 conversations new10 conversations new
10 conversations new
 
Configuration for Java EE: Config JSR and Tamaya
Configuration for Java EE: Config JSR and TamayaConfiguration for Java EE: Config JSR and Tamaya
Configuration for Java EE: Config JSR and Tamaya
 
11 transitive persistence and filters
11 transitive persistence and filters11 transitive persistence and filters
11 transitive persistence and filters
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Jfxpub binding
Jfxpub bindingJfxpub binding
Jfxpub binding
 
Al rihieli persistence
Al rihieli persistenceAl rihieli persistence
Al rihieli persistence
 
Oracle
Oracle Oracle
Oracle
 
EJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLinkEJB 3.0 Java Persistence with Oracle TopLink
EJB 3.0 Java Persistence with Oracle TopLink
 
Jpa
JpaJpa
Jpa
 
159747608 a-training-report-on
159747608 a-training-report-on159747608 a-training-report-on
159747608 a-training-report-on
 
Comparing glassfish-jboss
Comparing glassfish-jbossComparing glassfish-jboss
Comparing glassfish-jboss
 
Lo nuevo en Spring 3.0
Lo nuevo  en Spring 3.0Lo nuevo  en Spring 3.0
Lo nuevo en Spring 3.0
 
Spring Framework 5.0: Hidden Gems
Spring Framework 5.0: Hidden GemsSpring Framework 5.0: Hidden Gems
Spring Framework 5.0: Hidden Gems
 
Java API for JSON Binding - Introduction and update
Java API for JSON Binding - Introduction and updateJava API for JSON Binding - Introduction and update
Java API for JSON Binding - Introduction and update
 
Change Management and Versioning in Ontologies
Change Management and Versioning in OntologiesChange Management and Versioning in Ontologies
Change Management and Versioning in Ontologies
 
05 inheritance
05 inheritance05 inheritance
05 inheritance
 
Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6Introduction to CDI and DI in Java EE 6
Introduction to CDI and DI in Java EE 6
 

Último

Último (20)

The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

15 jpaql

  • 1. Professional Open Source™ JPA QL © JBoss, Inc. 2003, 2004. 07/17/04 1
  • 2. Query API Professional Open Source™  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. © JBoss, Inc. 2003, 2004. 2
  • 3. Defining named queries Professional Open Source™  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. © JBoss, Inc. 2003, 2004. 3
  • 4. Creating a query instance Professional Open Source™ The EntityManager interface provides several methods to create queries using either JPQL or native SQL statements. © JBoss, Inc. 2003, 2004. 4
  • 5. Creating a named query instance Professional Open Source™ © JBoss, Inc. 2003, 2004. 5
  • 6. Creating a dynamic query instance Professional Open Source™ © JBoss, Inc. 2003, 2004. 6
  • 7. Setting parameters for a query Professional Open Source™ Setting Positional Parameters Setting Named parameters © JBoss, Inc. 2003, 2004. 7
  • 8. Retrieving a single entity Professional Open Source™ 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 not retrieve 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. © JBoss, Inc. 2003, 2004. 8
  • 9. Retrieving a collection of entities Professional Open Source™ 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 an active transaction, and if one isn’t available, the retrieved entities will be detached after retrieval. © JBoss, Inc. 2003, 2004. 9
  • 10. Paginating through a result list Professional Open Source™ © JBoss, Inc. 2003, 2004. 10
  • 11. Specifying query hints Professional Open Source™  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. © JBoss, Inc. 2003, 2004. 11
  • 12. Specifying Query Hints in a NamedQuery Professional Open Source™ © JBoss, Inc. 2003, 2004. 12
  • 13. Professional Open Source™ Introduction to JPQL © JBoss, Inc. 2003, 2004. 13
  • 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. © JBoss, Inc. 2003, 2004. 14
  • 15. How is JPQL Different from SQL? Professional Open Source™  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. © JBoss, Inc. 2003, 2004. 15
  • 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. © JBoss, Inc. 2003, 2004. 16
  • 17. Defining and using SELECT Professional Open Source™ © JBoss, Inc. 2003, 2004. 17
  • 18. Defining UPDATE and DELETE Professional Open Source™ © JBoss, Inc. 2003, 2004. 18
  • 19. Identifying the query domain: naming an entity Professional Open Source™ © JBoss, Inc. 2003, 2004. 19
  • 20. What is a path expression? Professional Open Source™  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 © JBoss, Inc. 2003, 2004. 20
  • 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 © JBoss, Inc. 2003, 2004. 21
  • 22. Conditional expressions and operators Professional Open Source™  A condition in the WHERE clause that filters results from a query is known as a conditional expression.  Operators supported by JPQL : © JBoss, Inc. 2003, 2004. 22
  • 23. Using a range with BETWEEN Professional Open Source™ © JBoss, Inc. 2003, 2004. 23
  • 24. Using the IN operator Professional Open Source™ © JBoss, Inc. 2003, 2004. 24
  • 25. Using the LIKE operator Professional Open Source™ © JBoss, Inc. 2003, 2004. 25
  • 26. Dealing with null values and empty collections Professional Open Source™  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 © JBoss, Inc. 2003, 2004. 26
  • 27. Checking for the existence of an entity in a collection Professional Open Source™ © JBoss, Inc. 2003, 2004. 27
  • 28. String functions Professional Open Source™ © JBoss, Inc. 2003, 2004. 28
  • 29. Arithmetic functions Professional Open Source™ © JBoss, Inc. 2003, 2004. 29
  • 30. JPQL temporal functions Professional Open Source™ © JBoss, Inc. 2003, 2004. 30
  • 31. Aggregate functions Professional Open Source™ © JBoss, Inc. 2003, 2004. 31
  • 32. Grouping with GROUP BY and HAVING Professional Open Source™ © JBoss, Inc. 2003, 2004. 32
  • 33. Ordering the query result Professional Open Source™ © JBoss, Inc. 2003, 2004. 33
  • 34. Using subqueries Professional Open Source™ © JBoss, Inc. 2003, 2004. 34
  • 35. Subqueries using ANY,ALL,SOME Professional Open Source™ © JBoss, Inc. 2003, 2004. 35
  • 36. Theta-joins Professional Open Source™ © JBoss, Inc. 2003, 2004. 36
  • 37. Inner Join Professional Open Source™  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 © JBoss, Inc. 2003, 2004. 37
  • 38. Outer joins Professional Open Source™ © JBoss, Inc. 2003, 2004. 38
  • 39. Fetch joins Professional Open Source™ © JBoss, Inc. 2003, 2004. 39
  • 40. Native SQL queries Professional Open Source™  Using dynamic queries with native SQL © JBoss, Inc. 2003, 2004. 40
  • 41. Using a named native SQL query Professional Open Source™ © JBoss, Inc. 2003, 2004. 41