Die Präsentation wird geladen. Bitte warten

Die Präsentation wird geladen. Bitte warten

Zühl, Kühle WS 08/09Java Persistence Query Language JPQL Java Persistence Query Language Cornelius Zühl Arend Kühle Komponentenarchitektur WS 08/09.

Ähnliche Präsentationen


Präsentation zum Thema: "Zühl, Kühle WS 08/09Java Persistence Query Language JPQL Java Persistence Query Language Cornelius Zühl Arend Kühle Komponentenarchitektur WS 08/09."—  Präsentation transkript:

1 Zühl, Kühle WS 08/09Java Persistence Query Language JPQL Java Persistence Query Language Cornelius Zühl Arend Kühle Komponentenarchitektur WS 08/09

2 Zühl, Kühle WS 08/09Java Persistence Query Language Java Persistence API JPA import javax.persistence Persistence Entity Objektrelationale Metadaten –Java-Annotationen oder XML-Datei Java Persistence Query Language (JPQL) –ähneln syntaktisch SQL-Abfragen –bezieht sich auf Entitäten

3 Zühl, Kühle WS 08/09Java Persistence Query Language Java Persistence API JPA EntityManager JPQL Query API Objektoperationen find merge persist update remove Entity persistance.xml Oracle TopLink Hibernate

4 Zühl, Kühle WS 08/09Java Persistence Query Language JPQL Syntax Erweitert um Objektorientierte Befehle is empty, is null, not … select u from User u Query SQL Syntaxkriterien werden genutzt and or = ( ) select u.name from User u where (u.age > 18 and u.age <= 65) or u.age = 83

5 Zühl, Kühle WS 08/09Java Persistence Query Language JPQL Statement Query q = em.createQuery (select u from User u"); List userList = q.getResultList(); Query q = em.createQuery ("delete u from User u where u.name = Busch "); int deleted = q.executeUpdate(); Query q = em.createQuery ("update User u set u.name = Max where u.name = Moritz"); int updated = q.executeUpdate(); selectupdatedelete JPQL Statement

6 Zühl, Kühle WS 08/09Java Persistence Query Language Datenabfrage Query API Primary Key JPQL Query NamedQuery NativeQuery EntityManager em; em.find(...); q = em.createQuery(...) Query q; q = em.createNativeQuery(...) q = em.createNamedQuery(...) EntityManager Datenabfrage

7 Zühl, Kühle WS 08/09Java Persistence Query Language JPQL Funktionen select u from User u where concat(u.name, 's') = 'Walters' select u from User u where substring(u.name, 1, 1) = 'W' select u from User u where trim(leading 'a' FROM u.name) = 'W' select u from User u where lower(u.name) = 'walter' select u from User u where upper(u.name) = 'WALTER' select u from User u where length(u.name) = 6 select u from User u where locate('a', u.name) = 2 select u from User u where abs(u.age) >= 5.00 select u from User u where sqrt(u.age) >= 1000 select u from User u where mod(u.age, 10) = 0

8 Zühl, Kühle WS 08/09Java Persistence Query Language Aggregatsfunktionen MIN, MAX, AVG, COUNT und SUM Query q = em.createQuery("select min(u.age), max(u.age), avg(u.age), sum(u.age) from User u); Object[] stats = (Object[]) q.getSingleResult();

9 Zühl, Kühle WS 08/09Java Persistence Query Language Polimorphische Queries FiatOpelRenault Auto Query q = em.createQuery (select a from Auto a where a.price autoList = q.getResultList();

10 Zühl, Kühle WS 08/09Java Persistence Query Language Prepared Statements em.createQuery("select u from User u where name = " +name+ " and age > " +age); public List findUsers(String name, int age) { Query q = em.createQuery(" Select u from User u where name = :name and age > :age"); q.setParameter("name", name); q.setParameter("age", age); return List users = q.getResultList(); } public User findUserByAddress(Address address) { Query q = em.createQuery(" select u from User u where u.address = :address "); q.setParameter(address", adedress); return q.getSingleResult(); } public List findUsers(String name, int age) { Query q = em.createQuery(" select u from User u where name = ?1 and age > ?2"); q.setParameter(1, name); q.setParameter(2, age); return List users = q.getResultList(); } Entity Object

11 Zühl, Kühle WS 08/09Java Persistence Query Language Begrenzen der Ergebnismenge Query q = em.createQuery("select u from User u"); q.setFirstResult(30); q.setMaxResults(10); List users = q.getResultList(); public List findUsers(int firstRow, int maxResults) { Query q = em.createQuery("select u from User u"); q.setFirstResult(firstRow); q.setMaxResults(maxResults); return List users = q.getResultList(); }

12 Zühl, Kühle WS 08/09Java Persistence Query Language Konkrete Daten statt komplette Objekte Query q = em.createQuery("select u.id, u.name from User u"); List result = q.getResultList(); List users = new ArrayList (result.size()); for(Object[] objAr : result){ Long userId = objAr[0]; String userName = objAr[1]; User u = new User(); u.setUserId(userId); u.setUserName(userName); users.add(u); } public class User { public User(String id, String name) { this.id = id; this.name = name; }} Query q = em.createQuery("select new User(u.id, u.name) from User u"); List result = q.getResultList();

13 Zühl, Kühle WS 08/09Java Persistence Query Language (name=findAllUsers", query="select u from User u") public class User { (name=findAllUsers", query="select u from User (name=findAdults", query="select u from User u where age > (name=getAverageAge", query="select avg(u.age) from User u") }) public class User { //... } public List getUser() { Query query = em.createNamedQuery("findAllUsers"); // Query query = em.createNamedQuery("User.findAllUsers"); List userList = query.getResultList(); return userList; }

14 Zühl, Kühle WS 08/09Java Persistence Query Language Exception Handling getSingleResult() javax.persistence.NoResultException javax.persistence.NoneUniqueResultException java.lang.IllegalStateException getResultList() java.lang.IllegalStateException

15 Zühl, Kühle WS 08/09Java Persistence Query Language Und nun zur Praxis Vorführung Tools Queries Tipps Quellen


Herunterladen ppt "Zühl, Kühle WS 08/09Java Persistence Query Language JPQL Java Persistence Query Language Cornelius Zühl Arend Kühle Komponentenarchitektur WS 08/09."

Ähnliche Präsentationen


Google-Anzeigen