JPQL Java Persistence Query Language. Introduction The Java Persistence API specifies a query language that allows to define queries over entities and.

Slides:



Advertisements
Similar presentations
Connecting to Databases. relational databases tables and relations accessed using SQL database -specific functionality –transaction processing commit.
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
Persistence Jim Briggs 1. 2 Database connectivity: JDBC Java Database Connectivity An API for connecting Java programs (applications, applets and servlets)
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Java Persistence API part 2 INFORMATICS ENGINEERING – UNIVERSITY OF BRAWIJAYA Eriq Muhammad Adams J
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Introduction to SQL Programming Techniques.
Advanced Java programming (Java EE)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
1 Design patterns Lecture 4. 2 Three Important skills Understanding OO methodology Mastering Java language constructs Recognizing common problems and.
Introduction to Structured Query Language (SQL)
The Java Persistence API Edel Sherratt. Contents Revisit applications programming Using Java Persistence API.
1 Lecture 19 George Koutsogiannakis/Summer 2011 CS441 CURRENT TOPICS IN PROGRAMMING LANGUAGES.
Introduction to JPA Java Persistence API Introduction to JPA.
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
CSE446 S OFTWARE Q UALITY M ANAGEMENT Spring 2014 Yazılım ve Uyguluma Geliştirme Yöneticisi Orhan Başar Evren.
Object Query Language OQL
Maven for building Java applications By Nalin De Zoysa
Java Persistence API Mario Peshev National Academy for Software Development academy.devbg.org Svetlin Nakov National Academy for Software Development academy.devbg.org.
Database Systems: Design, Implementation, and Management Tenth Edition Chapter 8 Advanced SQL.
Java Database Connectivity (JDBC) Introduction to JDBC JDBC is a simple API for connecting from Java applications to multiple databases. Lets you smoothly.
Dynamic Data Exchanges with the Java Flow Processor Presenter: Scott Bowers Date: April 25, 2007.
NHibernate in Action Web Seminar at UMLChina By Pierre Henri Kuaté 2008/08/27
1 Java Persistence API JPA. 2 Agenda Motivation JPA Entities EntityManager & the Persistent Context Persistence Units Exceptions JPA Query Language.
Java Persistence API part.2 Eriq Muhammad Adams J |
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Introduction to LINQ Lecture # 19 August Introduction How do you interrogate/manipulate data? What if you could do the work in a type-safe," string-free.
Object Oriented Analysis and Design 1 Chapter 7 Database Design  UML Specification for Data Modeling  The Relational Data Model and Object Model  Persistence.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Java Database Connectivity (JDBC). Topics 1. The Vendor Variation Problem 2. SQL and Versions of JDBC 3. Creating an ODBC Data Source 4. Simple Database.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Efficient RDF Storage and Retrieval in Jena2 Written by: Kevin Wilkinson, Craig Sayers, Harumi Kuno, Dave Reynolds Presented by: Umer Fareed 파리드.
Middleware Technology (J2EE/EJB) Entity Bean (JBoss EJB 3.0 tutorial)
JPA Java Persistence API. Introduction The Java Persistence API provides an object/relational mapping facility for managing relational data in Java applications.
Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Dynamic SQL. 2 home back first prev next last What Will I Learn? Recall the stages through which all SQL statements pass Describe the reasons for using.
Faculty of Information Technology Advanced Java programming (J2EE) Java Persistence API Based on the JPA presentation from javabeat.net.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Oracle Data Integrator User Functions, Variables and Advanced Mappings
JDBC CS 260 Database Systems. Overview  Introduction  JDBC driver types  Eclipse project setup  Programming with JDBC  Prepared statements  SQL.
Query Optimization CMPE 226 Database Systems By, Arjun Gangisetty
The Java Persistence API ©SoftMoore ConsultingSlide 1.
By Srinivas Mahakud Java Persistence API JPA. Review Topics: Introduction what is the JPA? What is the JPA contains of? Why developers should Create the.
JPA The New Enterprise Persistence Standard Mike Keith
IS-907 Java EE Introduction to JPA. Java Persistence API A framework for using relational databases in Java programs mapping between tables and classes,
Lu Wei1 Outline Introduction Basic SQL Setting Up and Using PostgreSQL Advanced SQL Embeded SQL.
Topic : JPA Criteria API Kaster Nurmukan. Powerful Query Capabilities HQL Prototypical Use of Query API Criteria Queries Prototypical Use of Criteria.
Introduction to ORM Hibernate Hibernate vs JDBC. May 12, 2011 INTRODUCTION TO ORM ORM is a programming technique for converting data between relational.
ORM Basics Repository Pattern, Models, Entity Manager Ivan Yonkov Technical Trainer Software University
CS520 Web Programming Object-Relational Mapping with Hibernate and JPA (I) Chengyu Sun California State University, Los Angeles.
Copyright © 2016 Ramez Elmasri and Shamkant B. Navathe.
OOPSLA Lab1 Chapter 7 Java Binding Prof. Hyoung-Joo Kim OOPSLA Lab. Dept. of Computer Engineering Seoul National University.
1. Advanced SQL Functions Procedural Constructs Triggers.
6 Copyright © 2009, Oracle. All rights reserved. Using Dynamic SQL.
Managing Persistent Objects. Contents  Entity Manager  JPQL  Queries  Concurrency.
Chengyu Sun California State University, Los Angeles
Running a Forms Developer Application
Introduction to Entity Framework
Web Technologies IT230 Dr Mohamed Habib.
ATS Application Programming: Java Programming
Database JDBC Overview CS Programming Languages for Web Applications
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction To Codeigniter
PL/SQL Scripting in Oracle:
Object Oriented Programming in java
Chapter 8 Advanced SQL.
Chengyu Sun California State University, Los Angeles
Database SQL.
Presentation transcript:

JPQL Java Persistence Query Language

Introduction The Java Persistence API specifies a query language that allows to define queries over entities and their persistent state JPQL is an extension of EJB QL More robust flexible and object-oriented than SQL The persistence engine parse the query string, transform the JPQL to the native SQL before executing it

Creating Queries Query instances are obtained using: EntityManager.createQuery ( dynamic query) EntityManager.createNamedQuery (static query) Query API: getResultList() – execute query returning multiple results getSingleResult() – execute query returning single result executeUpdate() – execute bulk update or delete setFirstResult() – set the first result to retrieve setMaxResults() – set the maximum number of results to retrieve setParameter() – bind a value to a named or positional parameter setHint() – apply a vendor-specific hint to the query setFlushMode() – apply a flush mode to the query when it gets run

Static (Named) Queries Defined statically with the help annotation together with the entity elements: name - the name of the query that will be used with the createNamedQuery method query – query query="SELECT c FROM Customer") Query findAllQuery = entityManager.createNamedQuery(“findAllCustomers”); List customers = findAllQuery.getResultList();

Multiple Named Queries Multiple named queries can be logically defined with the help = “Mobile.selectAllQuery” query = “SELECT M FROM = “Mobile.deleteAllQuery” query = “DELETE M FROM MOBILEENTITY”) } )

Named Parameters Named parameters are parameters in a query that are prefixed with a colon (:) To bound parameter to an argument use method: Query.setParameter(String name, Object value) public List findWithName(String name) { return em.createQuery( "SELECT c FROM Customer c WHERE c.name LIKE :custName").setParameter("custName", name).getResultList(); }

Positional Parameters Positional parameters are prefixed with a question mark (?) followed the numeric position of the parameter in the query To set parameter values use method: Query.setParameter(integer position, Object value) public List findWithName(String name) { return em.createQuery( “SELECT c FROM Customer c WHERE c.name LIKE ?1”).setParameter(1, name).getResultList(); }

Dynamic Queries Dynamic queries are queries that are defined directly within an application’s business logic Worse efficiency and slower query execution, as the persistence engine has to do all the parsing and validation stuffs, along with mapping the JPQL to the SQL at the run-time public List findAll(String entityName){ return entityManager.createQuery( "select e from " + entityName + " e").getResultList(); }

Native Queries Queries may be expressed in native SQL Support for cases where it is necessary to use the native SQL of the target database in use Query q = em.createNativeQuery( "SELECT o.id, o.quantity, o.item " + "FROM Order o, Item i " + "WHERE (o.item = i.id) AND (i.name = 'widget')", annotaton is used for more advanced cases

Query Operations – Multiple Results Query.getResultList() will execute a query and may return a List object containing multiple entity instances Will return a non-parameterized List object Can only execute on select statements as opposed to UPDATE or DELETE statements For a statement other than SELECT run-time IllegalStateException will be thrown Query query = entityManager.createQuery(“SELECT C FROM CUSTOMER”); List mobiles = (List )query.getResultList();

Query Operations – Single Result A query that returns a single entity object If the match wasn’t successful, then EntityNotFoundException is returned If more than one matches occur during query execution a run-time exception NonUniqueResultException will be thrown Query singleSelectQuery = entityManager.createQuery( “SELECT C FROM CUSTOMER WHERE C.ID = ‘ABC-123’”); Customer custObj = singleSelectQuery.getSingleResult();

Paging Query Results int maxRecords = 10; int startPosition = 0; String queryString = “SELECT M FROM MOBILEENTITY”; while(true){ Query selectQuery = entityManager.createQuery(queryString); selectQuery.setMaxResults(maxRecords); selectQuery.setFirstResult(startPosition); List mobiles = entityManager.getResultList(queryString); if (mobiles.isEmpty()){ break; } process(mobiles); // process the mobile entities entityManager.clear();// detach the mobile objects startPosition = startPosition + mobiles.size(); }

Flushing Query Objects Two modes of flushing query objects AUTO (default) and COMMIT AUTO - any changes made to entity objects will be reflected the very next time when a SELECT query is made COMMIT - the persistence engine may only update all the state of the entities during the database COMMIT

JPQL Statement Language JPQL statement types: SELECT, UPDATE, DELETE Supported clauses: FROM WHERE GROUP_BY HAVING ORDER BY … Conditional expressions, aggregate functions,…

JPQL Enhancements over EJBQL 2.x Simplified query syntax JOIN operations Group By and Having Clause Subqueries Dynamic queries Named parameters Bulk update and delete

OO-style vs. SQL-style queries The main difference is that you query the application model, i.e. the entities, rather than any database tables Productivity can be increased if OO-style quries, e.g. employeeXYZ.getManager().getAddress() are automatically translated by the ORM engine into correct SQL code, e.g. SELECT t3.* FROM EMP t1, EMP t2, ADDR t3 WHERE t1.EMP_ID = “XYZ” AND t1.MGR_ID = t2.EMP_ID AND t2.ADDR_ID = t3.ADDR_ID Notice that the two-step object traversal was packed into a single DB query

Resursi JPQL Language Reference JPA Query API duction_to_java_persistence_api_jpa_ejb_3_0_6 duction_to_java_persistence_api_jpa_ejb_3_0_6 Standardizing Java Persistence with the EJB3 Java Persistence API – Query API with-ejb3-java-persistence-api.html?page=last&x- showcontent=text with-ejb3-java-persistence-api.html?page=last&x- showcontent=text