🗊Презентация 3. Java Persistence API. 4. Java Persistence Query Language

Нажмите для полного просмотра!
3. Java Persistence API. 4. Java Persistence Query Language, слайд №13. Java Persistence API. 4. Java Persistence Query Language, слайд №23. Java Persistence API. 4. Java Persistence Query Language, слайд №33. Java Persistence API. 4. Java Persistence Query Language, слайд №43. Java Persistence API. 4. Java Persistence Query Language, слайд №53. Java Persistence API. 4. Java Persistence Query Language, слайд №63. Java Persistence API. 4. Java Persistence Query Language, слайд №73. Java Persistence API. 4. Java Persistence Query Language, слайд №83. Java Persistence API. 4. Java Persistence Query Language, слайд №93. Java Persistence API. 4. Java Persistence Query Language, слайд №103. Java Persistence API. 4. Java Persistence Query Language, слайд №113. Java Persistence API. 4. Java Persistence Query Language, слайд №123. Java Persistence API. 4. Java Persistence Query Language, слайд №133. Java Persistence API. 4. Java Persistence Query Language, слайд №143. Java Persistence API. 4. Java Persistence Query Language, слайд №153. Java Persistence API. 4. Java Persistence Query Language, слайд №163. Java Persistence API. 4. Java Persistence Query Language, слайд №173. Java Persistence API. 4. Java Persistence Query Language, слайд №183. Java Persistence API. 4. Java Persistence Query Language, слайд №193. Java Persistence API. 4. Java Persistence Query Language, слайд №203. Java Persistence API. 4. Java Persistence Query Language, слайд №213. Java Persistence API. 4. Java Persistence Query Language, слайд №223. Java Persistence API. 4. Java Persistence Query Language, слайд №233. Java Persistence API. 4. Java Persistence Query Language, слайд №243. Java Persistence API. 4. Java Persistence Query Language, слайд №253. Java Persistence API. 4. Java Persistence Query Language, слайд №263. Java Persistence API. 4. Java Persistence Query Language, слайд №273. Java Persistence API. 4. Java Persistence Query Language, слайд №283. Java Persistence API. 4. Java Persistence Query Language, слайд №293. Java Persistence API. 4. Java Persistence Query Language, слайд №303. Java Persistence API. 4. Java Persistence Query Language, слайд №313. Java Persistence API. 4. Java Persistence Query Language, слайд №323. Java Persistence API. 4. Java Persistence Query Language, слайд №333. Java Persistence API. 4. Java Persistence Query Language, слайд №343. Java Persistence API. 4. Java Persistence Query Language, слайд №353. Java Persistence API. 4. Java Persistence Query Language, слайд №363. Java Persistence API. 4. Java Persistence Query Language, слайд №373. Java Persistence API. 4. Java Persistence Query Language, слайд №383. Java Persistence API. 4. Java Persistence Query Language, слайд №393. Java Persistence API. 4. Java Persistence Query Language, слайд №403. Java Persistence API. 4. Java Persistence Query Language, слайд №413. Java Persistence API. 4. Java Persistence Query Language, слайд №423. Java Persistence API. 4. Java Persistence Query Language, слайд №433. Java Persistence API. 4. Java Persistence Query Language, слайд №443. Java Persistence API. 4. Java Persistence Query Language, слайд №453. Java Persistence API. 4. Java Persistence Query Language, слайд №463. Java Persistence API. 4. Java Persistence Query Language, слайд №473. Java Persistence API. 4. Java Persistence Query Language, слайд №483. Java Persistence API. 4. Java Persistence Query Language, слайд №493. Java Persistence API. 4. Java Persistence Query Language, слайд №503. Java Persistence API. 4. Java Persistence Query Language, слайд №51

Содержание

Вы можете ознакомиться и скачать презентацию на тему 3. Java Persistence API. 4. Java Persistence Query Language. Доклад-сообщение содержит 51 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1





3. Java Persistence API
4. Java Persistence Query Language
Описание слайда:
3. Java Persistence API 4. Java Persistence Query Language

Слайд 2





Queries (1 of 2)
In JPA: SQL -> JP QL (Java Persistence Query Language)
A query is implemented in code as a Query or  TypedQuery object. They are constructed using the EntityManager as a factory
A query can be customized according to the needs of the application
Описание слайда:
Queries (1 of 2) In JPA: SQL -> JP QL (Java Persistence Query Language) A query is implemented in code as a Query or TypedQuery object. They are constructed using the EntityManager as a factory A query can be customized according to the needs of the application

Слайд 3





Queries (2 of 2)
A query can be issued at runtime by supplying the JP QL query criteria, or a criteria object. Example:
	TypedQuery<Merchant> query =  	em.createQuery("SELECT m FROM 			Merchant m", Merchant.class);
	List<Merchant> listM = null;
	listM = query.getResultList();
.  .  .  .  .  .  .  .  .  .  .  .
See P341SelectMerchant project for the full text
Описание слайда:
Queries (2 of 2) A query can be issued at runtime by supplying the JP QL query criteria, or a criteria object. Example: TypedQuery<Merchant> query = em.createQuery("SELECT m FROM Merchant m", Merchant.class); List<Merchant> listM = null; listM = query.getResultList(); . . . . . . . . . . . . See P341SelectMerchant project for the full text

Слайд 4





DAO & Service Interfaces
public interface MerchantDao {
	public Merchant findById(int id);
	public List<Merchant> findAll();
}
public interface MerchantService {
	public Merchant findById(int id);
	public List<Merchant> findAll();
}
Описание слайда:
DAO & Service Interfaces public interface MerchantDao { public Merchant findById(int id); public List<Merchant> findAll(); } public interface MerchantService { public Merchant findById(int id); public List<Merchant> findAll(); }

Слайд 5





MerchantDaoImpl Class
@Repository
public class MerchantDaoImpl implements MerchantDao{
    @PersistenceContext
    private EntityManager em;
   . . . . . . . . . . . .
    public List<Merchant> findAll(){
        TypedQuery<Merchant> query = 	  
              em.createQuery("SELECT m FROM Merchant m", 	   Merchant.class);
         List<Merchant> listM = null;
         listM = query.getResultList();
         return listM; }}
Описание слайда:
MerchantDaoImpl Class @Repository public class MerchantDaoImpl implements MerchantDao{ @PersistenceContext private EntityManager em; . . . . . . . . . . . . public List<Merchant> findAll(){ TypedQuery<Merchant> query = em.createQuery("SELECT m FROM Merchant m", Merchant.class); List<Merchant> listM = null; listM = query.getResultList(); return listM; }}

Слайд 6





MerchantServiceImpl Class
@Named
public class MerchantServiceImpl implements MerchantService{
    @Inject
    private MerchantDao merchantDao;
   . . . . . . . . . . . . .
    public List<Merchant> findAll(){
	return merchantDao.findAll();
    }
}
Описание слайда:
MerchantServiceImpl Class @Named public class MerchantServiceImpl implements MerchantService{ @Inject private MerchantDao merchantDao; . . . . . . . . . . . . . public List<Merchant> findAll(){ return merchantDao.findAll(); } }

Слайд 7





Main Class
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     MerchantService merchantService = 	context.getBean(MerchantService.class);
     List<Merchant> list = merchantService.findAll();
     for(Merchant m: list)
	System.out.println("name = " + m.getName() + "    	       charge = " +m.getCharge());
}
Описание слайда:
Main Class @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); MerchantService merchantService = context.getBean(MerchantService.class); List<Merchant> list = merchantService.findAll(); for(Merchant m: list) System.out.println("name = " + m.getName() + " charge = " +m.getCharge()); }

Слайд 8





Java Persistence Query Language 
Java Persistence Query Language (JP QL) is a database-independent query language that operates on the logical entity model as opposed to the physical data model
Queries may also be expressed in SQL to take advantage of the underlying database
The key difference between SQL and JP QL is that instead of selecting from a table, an entity from the application domain model has been specified instead
Описание слайда:
Java Persistence Query Language Java Persistence Query Language (JP QL) is a database-independent query language that operates on the logical entity model as opposed to the physical data model Queries may also be expressed in SQL to take advantage of the underlying database The key difference between SQL and JP QL is that instead of selecting from a table, an entity from the application domain model has been specified instead

Слайд 9





Filtering Results
JP QL supports the WHERE clause to set conditions on the data being returned
Majority of operators commonly available in SQL are available in JP QL:
basic comparison operators
IN expression
LIKE expression
BETWEEN expression
subqueries
Описание слайда:
Filtering Results JP QL supports the WHERE clause to set conditions on the data being returned Majority of operators commonly available in SQL are available in JP QL: basic comparison operators IN expression LIKE expression BETWEEN expression subqueries

Слайд 10





Exercise: Find Payments
Find all payments to the given merchant
Описание слайда:
Exercise: Find Payments Find all payments to the given merchant

Слайд 11





DAO & Service Interfaces
public interface PaymentDao {
	public List<Payment> findByMerchantId(int id);
}
public interface PaymentService {
	public List<Payment> findByMerchantId(int id);
}
Описание слайда:
DAO & Service Interfaces public interface PaymentDao { public List<Payment> findByMerchantId(int id); } public interface PaymentService { public List<Payment> findByMerchantId(int id); }

Слайд 12





PaymentDaoImpl Class
@Repository
public class PaymentDaoImpl implements PaymentDao{
    @PersistenceContext
    private EntityManager em;
    
    public List<Payment> findByMerchantId(int id){
        TypedQuery<Payment> query = 	em.createQuery("SELECT p FROM Payment p 	WHERE p.merchantId = " + id, Payment.class);
        return query.getResultList();
    }
}
Описание слайда:
PaymentDaoImpl Class @Repository public class PaymentDaoImpl implements PaymentDao{ @PersistenceContext private EntityManager em; public List<Payment> findByMerchantId(int id){ TypedQuery<Payment> query = em.createQuery("SELECT p FROM Payment p WHERE p.merchantId = " + id, Payment.class); return query.getResultList(); } }

Слайд 13





PaymentServiceImpl Class
@Named
public class PaymentServiceImpl implements PaymentService{
    @Inject
    private PaymentDao paymentDao;
    
    public List<Payment> findByMerchantId(int id){
    	return paymentDao.findByMerchantId(id);
    }
}
Описание слайда:
PaymentServiceImpl Class @Named public class PaymentServiceImpl implements PaymentService{ @Inject private PaymentDao paymentDao; public List<Payment> findByMerchantId(int id){ return paymentDao.findByMerchantId(id); } }

Слайд 14





Main Class
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     PaymentService paymentService = 	context.getBean(PaymentService.class);
     List<Payment> list = paymentService.findByMerchantId(3);
     for(Payment p: list)
	System.out.println(p.toString()); 
}
Описание слайда:
Main Class @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); PaymentService paymentService = context.getBean(PaymentService.class); List<Payment> list = paymentService.findByMerchantId(3); for(Payment p: list) System.out.println(p.toString()); }

Слайд 15





Exercise: Find Payments
See P342PaymentsWhere project for the full text
Описание слайда:
Exercise: Find Payments See P342PaymentsWhere project for the full text

Слайд 16





Joins Between Entities 
 Just as with SQL and tables, if we want to navigate along a collection association and return elements of that collection, we must join the two entities together
In JP QL, joins may also be expressed in the FROM clause using the JOIN operator
Описание слайда:
Joins Between Entities Just as with SQL and tables, if we want to navigate along a collection association and return elements of that collection, we must join the two entities together In JP QL, joins may also be expressed in the FROM clause using the JOIN operator

Слайд 17





Join Example
Get names of customers who payed more then 500.0 by the time
Описание слайда:
Join Example Get names of customers who payed more then 500.0 by the time

Слайд 18





DAO & Service Interfaces
public interface CustomerDao {
	public Customer findById(int id);
   	. . . . . . . . . . . . . .
	public List<String> getNames(double sumPayed);
}
public interface CustomerService {
	public Customer findById(int id);
 	. . . . . . . . . . . . . .
	public List<String> getNames(double sumPayed);
}
Описание слайда:
DAO & Service Interfaces public interface CustomerDao { public Customer findById(int id); . . . . . . . . . . . . . . public List<String> getNames(double sumPayed); } public interface CustomerService { public Customer findById(int id); . . . . . . . . . . . . . . public List<String> getNames(double sumPayed); }

Слайд 19





CustomerDaoImpl Class
public List<String> getNames(double sumPayed){
      String txt = "SELECT DISTINCT c.name FROM ";   
      txt += "Payment p, Customer c " ;
      txt += "WHERE c.id = p.customerId AND p.sumPayed > 	" 	+ sumPayed;
      TypedQuery<String> query = em.createQuery(txt, String.class);
      return query.getResultList();
}
Описание слайда:
CustomerDaoImpl Class public List<String> getNames(double sumPayed){ String txt = "SELECT DISTINCT c.name FROM "; txt += "Payment p, Customer c " ; txt += "WHERE c.id = p.customerId AND p.sumPayed > " + sumPayed; TypedQuery<String> query = em.createQuery(txt, String.class); return query.getResultList(); }

Слайд 20





CustomerServiceImpl Class
public List<String> getNames(double sumPayed){
    return customerDao.getNames(sumPayed);
}
Описание слайда:
CustomerServiceImpl Class public List<String> getNames(double sumPayed){ return customerDao.getNames(sumPayed); }

Слайд 21





Main Class
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     CustomerService customerService = 	context.getBean(CustomerService.class);
     List<String> list = customerService.getNames(500.0);
     for(String s: list)
	System.out.println(s); 
}
Описание слайда:
Main Class @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); CustomerService customerService = context.getBean(CustomerService.class); List<String> list = customerService.getNames(500.0); for(String s: list) System.out.println(s); }

Слайд 22





Join Example
See P343PaymentJoin project for the full text
Описание слайда:
Join Example See P343PaymentJoin project for the full text

Слайд 23





Aggregate Queries
There are five supported aggregate functions (AVG, COUNT, MIN, MAX, SUM)
Results may be grouped in the GROUP BY clause and filtered using the HAVING clause.
Описание слайда:
Aggregate Queries There are five supported aggregate functions (AVG, COUNT, MIN, MAX, SUM) Results may be grouped in the GROUP BY clause and filtered using the HAVING clause.

Слайд 24





Aggregate Example
Find the sum of all payments
Описание слайда:
Aggregate Example Find the sum of all payments

Слайд 25





DAO & Service Interfaces
public interface PaymentDao {
	public List<Payment> findByMerchantId(int id);
	public double getPaymentSum();
}
public interface PaymentService {
	public List<Payment> findByMerchantId(int id);
	public double getPaymentSum();
}
Описание слайда:
DAO & Service Interfaces public interface PaymentDao { public List<Payment> findByMerchantId(int id); public double getPaymentSum(); } public interface PaymentService { public List<Payment> findByMerchantId(int id); public double getPaymentSum(); }

Слайд 26





PaymentDaoImpl Class
public double getPaymentSum(){
    TypedQuery<Double> query = em.createQuery 	("SELECT SUM(p.sumPayed) FROM 	Payment p", Double.class);
    return query.getSingleResult();
}
Описание слайда:
PaymentDaoImpl Class public double getPaymentSum(){ TypedQuery<Double> query = em.createQuery ("SELECT SUM(p.sumPayed) FROM Payment p", Double.class); return query.getSingleResult(); }

Слайд 27





Main Class
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     PaymentService paymentService = 	context.getBean(PaymentService.class);
     double sum = paymentService.getPaymentSum();
     System.out.println("total = " + sum); 
}
Описание слайда:
Main Class @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); PaymentService paymentService = context.getBean(PaymentService.class); double sum = paymentService.getPaymentSum(); System.out.println("total = " + sum); }

Слайд 28





Aggregate Example
See P344Aggregation project for the full text
Описание слайда:
Aggregate Example See P344Aggregation project for the full text

Слайд 29





Query Positional Parameters
Parameters are indicated in the query string by a question mark followed by the parameter number
When the query is executed, the developer specifies the parameter number that should be replaced
Описание слайда:
Query Positional Parameters Parameters are indicated in the query string by a question mark followed by the parameter number When the query is executed, the developer specifies the parameter number that should be replaced

Слайд 30





DAO & Service Interfaces
public interface PaymentDao {
     public List<Payment> findByMerchantId(int id);
     public double getPaymentSum();
     public List<Payment> getLargePayments(double limit);
}
public interface PaymentService {
     public List<Payment> findByMerchantId(int id);
     public double getPaymentSum();
     public List<Payment> getLargePayments(double limit);
}
Описание слайда:
DAO & Service Interfaces public interface PaymentDao { public List<Payment> findByMerchantId(int id); public double getPaymentSum(); public List<Payment> getLargePayments(double limit); } public interface PaymentService { public List<Payment> findByMerchantId(int id); public double getPaymentSum(); public List<Payment> getLargePayments(double limit); }

Слайд 31





PaymentDaoImpl Class
public List<Payment> getLargePayments(double limit){
     TypedQuery<Payment> query = em.createQuery 	("SELECT p FROM Payment p WHERE 	p.sumPayed > ?1", Payment.class);
     query.setParameter(1, limit);
     return query.getResultList();
}
Описание слайда:
PaymentDaoImpl Class public List<Payment> getLargePayments(double limit){ TypedQuery<Payment> query = em.createQuery ("SELECT p FROM Payment p WHERE p.sumPayed > ?1", Payment.class); query.setParameter(1, limit); return query.getResultList(); }

Слайд 32





Main Class
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     PaymentService paymentService = 	context.getBean(PaymentService.class);
     List<Payment> list = 	paymentService.getLargePayments(750.0);
     for (Payment p: list) 
	System.out.println(p.toString());
}
See P345Parameters project for the full text
Описание слайда:
Main Class @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); PaymentService paymentService = context.getBean(PaymentService.class); List<Payment> list = paymentService.getLargePayments(750.0); for (Payment p: list) System.out.println(p.toString()); } See P345Parameters project for the full text

Слайд 33





Query Named Parameters
Named parameters may also be used and are indicated in the query string by a colon followed by the parameter name
When the query is executed, the developer specifies the parameter name that should be replaced
Описание слайда:
Query Named Parameters Named parameters may also be used and are indicated in the query string by a colon followed by the parameter name When the query is executed, the developer specifies the parameter name that should be replaced

Слайд 34





PaymentDaoImpl Class
 public List<Payment> getLargePayments(double limit){
    TypedQuery<Payment> query = em.createQuery 	("SELECT p FROM Payment p WHERE 	p.sumPayed > :limit", Payment.class);
      query.setParameter("limit", limit);
      return query.getResultList();
}
See P245Parameters project for the full text
Описание слайда:
PaymentDaoImpl Class public List<Payment> getLargePayments(double limit){ TypedQuery<Payment> query = em.createQuery ("SELECT p FROM Payment p WHERE p.sumPayed > :limit", Payment.class); query.setParameter("limit", limit); return query.getResultList(); } See P245Parameters project for the full text

Слайд 35





Executing Queries
The TypedQuery interface provides three different ways to execute a query:
getSingleResult() - if the query is expected to return a single result 
getResultList() - if more than one result may be returned
executeUpdate() - is used to invoke bulk update and delete queries
Описание слайда:
Executing Queries The TypedQuery interface provides three different ways to execute a query: getSingleResult() - if the query is expected to return a single result getResultList() - if more than one result may be returned executeUpdate() - is used to invoke bulk update and delete queries

Слайд 36





getResultList() Method
Returns a collection containing the query results
If the query did not return any data, the collection is empty
The return type is specified as a List  instead of a Collection in order to support queries that specify a sort order
If the query uses the ORDER BY clause to specify a sort order, the results will be put into the result list in the same order
Описание слайда:
getResultList() Method Returns a collection containing the query results If the query did not return any data, the collection is empty The return type is specified as a List instead of a Collection in order to support queries that specify a sort order If the query uses the ORDER BY clause to specify a sort order, the results will be put into the result list in the same order

Слайд 37





Exercise: Sort Merchants
Create a project to sort merchants by the value of needToSend field
Описание слайда:
Exercise: Sort Merchants Create a project to sort merchants by the value of needToSend field

Слайд 38





DAO & Service Interfaces
public interface MerchantDao {
	public Merchant findById(int id);
	public List<Merchant> getSortedByNeedToPay();
}
public interface MerchantService {
	public Merchant findById(int id);
	public List<Merchant> getSortedByNeedToPay();
}
Описание слайда:
DAO & Service Interfaces public interface MerchantDao { public Merchant findById(int id); public List<Merchant> getSortedByNeedToPay(); } public interface MerchantService { public Merchant findById(int id); public List<Merchant> getSortedByNeedToPay(); }

Слайд 39





MerchantDaoImpl Class
public List<Merchant> getSortedByNeedToPay(){
    String txt = "SELECT m FROM Merchant m ORDER BY 	m.needToSend";
    TypedQuery<Merchant> query = em.createQuery(txt, 	Merchant.class);
    return query.getResultList();
}
Описание слайда:
MerchantDaoImpl Class public List<Merchant> getSortedByNeedToPay(){ String txt = "SELECT m FROM Merchant m ORDER BY m.needToSend"; TypedQuery<Merchant> query = em.createQuery(txt, Merchant.class); return query.getResultList(); }

Слайд 40





Main Class
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     MerchantService merchantService = 	context.getBean(MerchantService.class);
     List<Merchant> list = 	merchantService.getSortedByNeedToPay();
     for(Merchant m: list)
	System.out.println("name = " + m.getName() + "    	     sumToPay = " + m .getNeedToSend()); 
}
Описание слайда:
Main Class @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); MerchantService merchantService = context.getBean(MerchantService.class); List<Merchant> list = merchantService.getSortedByNeedToPay(); for(Merchant m: list) System.out.println("name = " + m.getName() + " sumToPay = " + m .getNeedToSend()); }

Слайд 41





Exercise: Sort Merchants
See P346Sort project for the full text
Описание слайда:
Exercise: Sort Merchants See P346Sort project for the full text

Слайд 42





getSingleResult() Method
Instead of iterating to the first result in a collection, the object is directly returned
Throws a NoResultException exception when no results are available
Throws a NonUniqueResultException  exception if multiple results are available after executing the query
Описание слайда:
getSingleResult() Method Instead of iterating to the first result in a collection, the object is directly returned Throws a NoResultException exception when no results are available Throws a NonUniqueResultException exception if multiple results are available after executing the query

Слайд 43





Working with Query Results
The result type of a query is determined by the expressions listed in the SELECT clause of the query:
Basic types, such as String, the primitive types, and JDBC types 
Entity types 
An array of Object   
User-defined types created from a constructor expression
Описание слайда:
Working with Query Results The result type of a query is determined by the expressions listed in the SELECT clause of the query: Basic types, such as String, the primitive types, and JDBC types Entity types An array of Object User-defined types created from a constructor expression

Слайд 44





Constructor expressions (1/2)
Provide developers with a way to map array of Object result types to custom objects
Typically this is used to convert the results into JavaBean-style classes that provide getters for the different returned values
A constructor expression is defined in JP QL using the NEW operator in the SELECT clause
Описание слайда:
Constructor expressions (1/2) Provide developers with a way to map array of Object result types to custom objects Typically this is used to convert the results into JavaBean-style classes that provide getters for the different returned values A constructor expression is defined in JP QL using the NEW operator in the SELECT clause

Слайд 45





Constructor expressions (2/2)
The argument to the NEW operator is the fully qualified name of the class that will be instantiated to hold the results for each row of data returned
The only requirement on this class is that it has a constructor with arguments matching the exact type and order that will be specified in the query.
Описание слайда:
Constructor expressions (2/2) The argument to the NEW operator is the fully qualified name of the class that will be instantiated to hold the results for each row of data returned The only requirement on this class is that it has a constructor with arguments matching the exact type and order that will be specified in the query.

Слайд 46





Example: Grouping Payments
Get general sum of charge for every merchant
Описание слайда:
Example: Grouping Payments Get general sum of charge for every merchant

Слайд 47





Class Result
public class Result {
	private String name;
	private double sum;
	public Result(){   }
	public Result(String name, double sum){
		this.name = name;
		this.sum = sum;
	}
	public String getName() { return name; }
     .     .     .     .     .     .     .
Описание слайда:
Class Result public class Result { private String name; private double sum; public Result(){ } public Result(String name, double sum){ this.name = name; this.sum = sum; } public String getName() { return name; } . . . . . . .

Слайд 48





DAO & Service Interfaces
public interface MerchantDao {
	public Merchant findById(int id);
	public List<Merchant> getSortedByNeedToPay();
	public List<Result> getTotalReport();
}
public interface MerchantService {
	public Merchant findById(int id);
	public List<Merchant> getSortedByNeedToPay();
	public List<Result> getTotalReport();
}
Описание слайда:
DAO & Service Interfaces public interface MerchantDao { public Merchant findById(int id); public List<Merchant> getSortedByNeedToPay(); public List<Result> getTotalReport(); } public interface MerchantService { public Merchant findById(int id); public List<Merchant> getSortedByNeedToPay(); public List<Result> getTotalReport(); }

Слайд 49





MerchantDaoImpl Class
public List<Result> getTotalReport(){
    String txt = "SELECT new com.bionic.edu.Result 	(m.name, SUM(p.chargePayed)) ";
    txt += "FROM Payment p, Merchant m WHERE m.id = 	p.merchantId GROUP BY m.name"; 
    TypedQuery<Result> query = em.createQuery(txt, 	Result.class);
    return query.getResultList();
}
Описание слайда:
MerchantDaoImpl Class public List<Result> getTotalReport(){ String txt = "SELECT new com.bionic.edu.Result (m.name, SUM(p.chargePayed)) "; txt += "FROM Payment p, Merchant m WHERE m.id = p.merchantId GROUP BY m.name"; TypedQuery<Result> query = em.createQuery(txt, Result.class); return query.getResultList(); }

Слайд 50





Main Class
@SuppressWarnings("resource")
public static void main(String[] args) {
     ApplicationContext context = new 	ClassPathXmlApplicationContext("beans.xml");
     MerchantService merchantService = 	context.getBean(MerchantService.class);
     List<Result> list = merchantService.getTotalReport();
     for(Result r: list)
	System.out.format("%1$25s  %2$8.2f \n", 	     		r.getName(), r.getSum()); 
}
Описание слайда:
Main Class @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); MerchantService merchantService = context.getBean(MerchantService.class); List<Result> list = merchantService.getTotalReport(); for(Result r: list) System.out.format("%1$25s %2$8.2f \n", r.getName(), r.getSum()); }

Слайд 51





Example: Grouping Payments
See P347Grouping project for the full text
Описание слайда:
Example: Grouping Payments See P347Grouping project for the full text



Похожие презентации
Mypresentation.ru
Загрузить презентацию