Téléchargeur: Ethan
Pays: Greece
Temps de chargement: Sep 26, 2018
Prix: Gratuit
Évaluation: Basé sur 1 utilisateurs

S'il vous plaît, vérifiez que vous n'êtes pas un robot pour charger le reste des pages

tÉlÉcharger jpql tutorial pdf

JPA - JPQL
http://www.tutorialspoint.com/jpa/jpa_jpql.htm
Copyright © tutorialspoint.com
This chapter tells you about JPQL and how it works with persistence units. In this chapter, examples
follow the same package hierarchy, which we used in the previous chapter as follows:
Java Persistence Query language
JPQL is Java Persistence Query Language defined in JPA specification. It is used to create queries
against entities to store in a relational database. JPQL is developed based on SQL syntax. But it
won’t affect the database directly.
JPQL can retrieve information or data using SELECT clause, can do bulk updates using UPDATE
clause and DELETE clause. EntityManager.createQuery API will support for querying language.
Query Structure
JPQL syntax is very similar to the syntax of SQL. Having SQL like syntax is an advantage because
SQL is a simple structured query language and many developers are using it in applications. SQL
works directly against relational database tables, records and fields, whereas JPQL works with Java
classes and instances.
For example, a JPQL query can retrieve an entity object rather than field result set from database,
as with SQL. The JPQL query structure as follows.
SELECT
[WHERE
[GROUP
[ORDER
... FROM ...
...]
BY ... [HAVING ...]]
BY ...]
The structure of JPQL DELETE and UPDATE queries is simpler as follows.
DELETE FROM ... [WHERE ...]
UPDATE ... SET ... [WHERE ...]
Scalar and Aggregate Functions
Scalar functions returns resultant values based on input values. Aggregate functions returns the
resultant values by calculating the input values.
Follow the same example employee management used in previous chapters. Here we will go
through the service classes using scalar and aggregate functions of JPQL.
Let us assume the jpadb.employee table contains following records.
Eid
Ename
Salary
Deg
1201
Gopal
40000
Technical Manager
1202
Manisha
40000
Proof Reader
1203
Masthanvali
40000
Technical Writer
1204
Satish
30000
Technical Writer
1205
Krishna
30000
Technical Writer
1206
Kiran
35000
Proof Reader
Create a class named ScalarandAggregateFunctions.java under
com.tutorialspoint.eclipselink.service package as follows.
package com.tutorialspoint.eclipselink.service;
import
import
import
import
import
java.util.List;
javax.persistence.EntityManager;
javax.persistence.EntityManagerFactory;
javax.persistence.Persistence;
javax.persistence.Query;
public class ScalarandAggregateFunctions {
public static void main( String[ ] args ) {
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory(
"Eclipselink_JPA" );
EntityManager entitymanager = emfactory.createEntityManager();
//Scalar function
Query query = entitymanager.
createQuery("Select UPPER(e.ename) from Employee e");
List list = query.getResultList();
for(String e:list) {
System.out.println("Employee NAME :"+e);
}
//Aggregate function
Query query1 = entitymanager.createQuery("Select MAX(e.salary) from Employee e");
Double result = (Double) query1.getSingleResult();
System.out.println("Max Employee Salary :" + result);
}
}
After compilation and execution of the above program you will get output in the console panel of
Eclipse IDE as follows:
Employee NAME :GOPAL
Employee NAME :MANISHA
Employee NAME :MASTHANVALI
Employee NAME :SATISH