SlideShare una empresa de Scribd logo
1 de 29
Descargar para leer sin conexión
JPA - Java Persistence API
Thomas Wöhlke
ObjectCode GmbH
12.03.2009
JPA: Agenda
© 2009 ObjectCode GmbH
Domain Object Model
© 2009 ObjectCode GmbH
Object-Relational Mapping
Analogie: OO  RDB
 Klasse  Tabelle
 Objekt  Zeile
 Variable Spalte
 Wert  Feld
Domain Object Modell = ERD ?
© 2009 ObjectCode GmbH
O/R Impedance Mismatch
© 2009 ObjectCode GmbH
O/R Impedance Mismatch
© 2009 ObjectCode GmbH
Domain Object Model: GLE
© 2009 ObjectCode GmbH
... und die Physik?
• v(Harddisk) << v(RAM)
• CPU 99% idle
• Process 99% IO_WAIT
• Page Impressions  SQL-Requests?
© 2009 ObjectCode GmbH
Anno Domini 2004...
© 2009 ObjectCode GmbH
© 2004-2005 TheServerside.com
Hibernate
Mapping von POJO‘s:
2. Java Bean API
3. Collection API (Generics)
4. Mapping:
XML oder Hibernate-Annotations
Hibernate ist ein JPA-Vendor:
 Hibernate-Core
 Hibernate-Annotations
 Hibernate Entity Manager
© 2009 ObjectCode GmbH
Von Hibernate nach JPA
© 2009 ObjectCode GmbH
JPA im JEE-Stack
© 2009 ObjectCode GmbH
persistence.xml (Java EE)
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="JPM_DB">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/JpmDS</jta-data-source>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<!-- These are the default for JBoss EJB3, but not for HEM: -->
<property name="hibernate.cache.provider_class"
value="org.hibernate.cache.HashtableCacheProvider"/>
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
</properties>
</persistence-unit>
</persistence>
© 2009 ObjectCode GmbH
persistence.xml (Java SE)
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="JPM_DB">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>org.woehlke.projecteering.kernel.calendar.pao.Day</class>
<class>org.woehlke.projecteering.kernel.minutes.pao.Event</class>
<class>org.woehlke.projecteering.kernel.minutes.pao.Minutes</class>
<class>org.woehlke.projecteering.kernel.minutes.pao.MinutesItem</class>
<class>org.woehlke.projecteering.kernel.projects.pao.Project</class>
<class>org.woehlke.projecteering.kernel.projects.pao.ProjectCategory</class>
<class>org.woehlke.projecteering.kernel.timerecording.pao.TimeRecordingItem</class>
<class>org.woehlke.projecteering.kernel.userrights.pao.Company</class>
<class>org.woehlke.projecteering.kernel.userrights.pao.Team</class>
<class>org.woehlke.projecteering.kernel.userrights.pao.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.generate_statistics" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.connection.username" value="jpm"/>
<property name="hibernate.connection.password" value="jpmpwd"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpm"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
</properties>
</persistence-unit>
</persistence>
© 2009 ObjectCode GmbH
Mapping der Klassen 1
public class Customer extends Person {
@OneToMany(mappedBy=“purchaser”) Set<Order> orders =
new HashSet<Order>();
protected Customer() { } // for loading from db
public Customer(String fname, String lname) {
super(fname, lname);
}
public void addOrder(Order o) { orders.add(o); }
public Set<Order> getOrders() { return orders; }
}
© 2009 ObjectCode GmbH
Mapping der Klassen 2
@Entity
@Table(name=“PRODUCTS”)
public class Product {
@Id @GeneratedValue
@Column(name=“PRODUCT_PK”)
long id;
@Version int oplock; // column defaults to “OPLOCK”
String name; // column defaults to “NAME”
@ManyToOne
@JoinColumn(name=“SUPP_FK”,
referencedColumnName=“SUPP_PK”)
Supplier supplier;
...
}
© 2009 ObjectCode GmbH
Mapping der Assoziationen
Kardinalität:
 1:1  OneToOne
 1:n  OneToMany
 n:m  ManyToMany
Richtung:
 1:n -> OneToMany
 N:1 <- ManyToOne
Sichtbarkeit:
 Unidirektional ->
 Bidirektional <->
© 2009 ObjectCode GmbH
Mapping der Vererbung
2. Eine Tabelle pro Klassen-Hierarchie
3. Eine Tabelle pro konkrete Klasse
4. Eine Tabelle pro Subklasse
5. Non-Entity Vererbung
6. Keine Vererbung: Embbeding
© 2009 ObjectCode GmbH
Mapping der Vererbung?
© 2009 ObjectCode GmbH
Einsatz von JPA im JBoss/EJB3
@Stateless
public class MinutesItemDao extends BaseDao<MinutesItem> implements
IMinutesItemDao {
@PersistenceContext(unitName = "JPM_DB")
private EntityManager entityManager;
public MinutesItem findById(Long id) {
return entityManager.find(MinutesItem.class,id);
}
public EntityManager getEntityManager() {
return entityManager;
}
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
}
© 2009 ObjectCode GmbH
Einsatz von JPA in Spring/Tomcat
@Transactional
public class MinutesDao extends BaseDao<Minutes> implements
IMinutesDao {
public Minutes findById(Long id) {
return jpaTemplate.find(Minutes.class,id);
}
}
© 2009 ObjectCode GmbH
EJB-QL
Query q = em.createQuery(“select c from Customer c where c.firstName
= :fname order by c.lastName”);
q.setParameter(“fname”, “Joe”);
q.setFirstResult(20);
q.setMaxResults(10);
List<Customer> customers = (List<Customer>) q.getResultList();
// all orders, as a named query
@Entity
@NamedQuery(name=“Order:findAllOrders”,
query=“select o from Order o”);
public class Order { ... }
Query q = em.createNamedQuery(“Order:findAllOrders”);
© 2009 ObjectCode GmbH
Lebenszyklus Persistente Objekte
2. Neu, transient (@Id id == null)
3. Persistent (@Id id != null)
4. Detached:
– Wie persistent (@Id id!= null)
– Jedoch ausserhalb des EntityManager Kontext
– Lazy Loading nicht möglich!
– Änderungen in DB sichern mit merge
© 2009 ObjectCode GmbH
EJB-QL
// all people, via a custom SQL statement
Query q = em.createNativeQuery(“SELECT ID, VERSION, SUBCLASS,
FIRSTNAME, LASTNAME FROM PERSON”, Person.class);
List<Person> people = (List<Person>) q.getResultList();
// single-result aggregate: average order total price
Query q = em.createQuery(“select avg(i.price) from Item i”);
Number avgPrice = (Number) q.getSingleResult();
// traverse to-many relations
Query q = em.createQuery(“select o from Order o left join o.items li where
li.price > :price”);
q.setParameter(“price”, 1000);
List<Order> orders = (List<Order>) q.getResultList();
© 2009 ObjectCode GmbH
Lazy Loading
• Supplier s = order.getItem().getProduct().getSupplier();
• Bei Aufruf eines Getters wird Objekt aus DB-Zeile nachgeladen.
• Ohne Lazy Loading muss komplettes Objekt-Netz geladen werden.
• Struktur des Objekt-Netzes variiert je nach Web-View
© 2009 ObjectCode GmbH
DAO und „Unit of Work“
© 2009 ObjectCode GmbH
Ausblick: Seam
• Kern-Entwickler von Hibernate sind nun im
Seam-Projekt
• O/R-Mapping von EJB3/JPA auch für die
Webapplikation
• OO im Datenbankbackend durch ORM
• OO im Webfrontend durch JSF
• Im Conversation-Scope ist Lazy-Loading möglich.
• Detached Objects können für die Webview
verwendet werden: Kein DTO-Antipattern
© 2009 ObjectCode GmbH
RTFM: http://www.hibernate.org/
O‘Reilly: Enterprise JavaBeans 3.0
Manning: EJB3 in Action
Manning: Hibernate in Action
Literatur
© 2009 ObjectCode GmbH
FRAGEN? Fragen!
... Vielen Dank
für die Aufmerksamkeit
© 2009 ObjectCode GmbH

Más contenido relacionado

La actualidad más candente

Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz SAurabh PRajapati
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework tola99
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate pptAneega
 
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 examplesecosio GmbH
 
Javascript under the hood 2
Javascript under the hood 2Javascript under the hood 2
Javascript under the hood 2Thang Tran Duc
 
Hibernate tutorial for beginners
Hibernate tutorial for beginnersHibernate tutorial for beginners
Hibernate tutorial for beginnersRahul Jain
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentationguest11106b
 
Introduction to graphQL
Introduction to graphQLIntroduction to graphQL
Introduction to graphQLMuhilvarnan V
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - CoreDzmitry Naskou
 
Karate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testingKarate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testingRoman Liubun
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced JavascriptAdieu
 
GraphQL IN Golang
GraphQL IN GolangGraphQL IN Golang
GraphQL IN GolangBo-Yi Wu
 
Functional Application Logging : Code Examples Using Spring Boot and Logback
Functional Application Logging : Code Examples Using Spring Boot and LogbackFunctional Application Logging : Code Examples Using Spring Boot and Logback
Functional Application Logging : Code Examples Using Spring Boot and LogbackMohammad Sabir Khan
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivityTanmoy Barman
 

La actualidad más candente (20)

Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz Java history, versions, types of errors and exception, quiz
Java history, versions, types of errors and exception, quiz
 
Spring Framework
Spring Framework  Spring Framework
Spring Framework
 
Hibernate ppt
Hibernate pptHibernate ppt
Hibernate ppt
 
Spring jdbc
Spring jdbcSpring jdbc
Spring jdbc
 
Java8 features
Java8 featuresJava8 features
Java8 features
 
Java Beans
Java BeansJava Beans
Java Beans
 
Introduction to OOP(in java) BY Govind Singh
Introduction to OOP(in java)  BY Govind SinghIntroduction to OOP(in java)  BY Govind Singh
Introduction to OOP(in java) BY Govind Singh
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
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
 
Javascript under the hood 2
Javascript under the hood 2Javascript under the hood 2
Javascript under the hood 2
 
Hibernate tutorial for beginners
Hibernate tutorial for beginnersHibernate tutorial for beginners
Hibernate tutorial for beginners
 
Hibernate Presentation
Hibernate  PresentationHibernate  Presentation
Hibernate Presentation
 
Introduction to graphQL
Introduction to graphQLIntroduction to graphQL
Introduction to graphQL
 
Spring Framework - Core
Spring Framework - CoreSpring Framework - Core
Spring Framework - Core
 
Karate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testingKarate - powerful and simple framework for REST API automation testing
Karate - powerful and simple framework for REST API automation testing
 
Advanced Javascript
Advanced JavascriptAdvanced Javascript
Advanced Javascript
 
GraphQL IN Golang
GraphQL IN GolangGraphQL IN Golang
GraphQL IN Golang
 
Functional Application Logging : Code Examples Using Spring Boot and Logback
Functional Application Logging : Code Examples Using Spring Boot and LogbackFunctional Application Logging : Code Examples Using Spring Boot and Logback
Functional Application Logging : Code Examples Using Spring Boot and Logback
 
Angular 9
Angular 9 Angular 9
Angular 9
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 

Similar a JPA - Java Persistence API

Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLsintelliyole
 
New Features of JSR 317 (JPA 2.0)
New Features of JSR 317 (JPA 2.0)New Features of JSR 317 (JPA 2.0)
New Features of JSR 317 (JPA 2.0)Markus Eisele
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesChoose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesCHOOSE
 
XML-Free Programming : Java Server and Client Development without &lt;>
XML-Free Programming : Java Server and Client Development without &lt;>XML-Free Programming : Java Server and Client Development without &lt;>
XML-Free Programming : Java Server and Client Development without &lt;>Arun Gupta
 
Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15Murat Yener
 
Eclipse World 2007: Fundamentals of the Eclipse Modeling Framework
Eclipse World 2007: Fundamentals of the Eclipse Modeling FrameworkEclipse World 2007: Fundamentals of the Eclipse Modeling Framework
Eclipse World 2007: Fundamentals of the Eclipse Modeling FrameworkDave Steinberg
 
EclipseCon 2008: Fundamentals of the Eclipse Modeling Framework
EclipseCon 2008: Fundamentals of the Eclipse Modeling FrameworkEclipseCon 2008: Fundamentals of the Eclipse Modeling Framework
EclipseCon 2008: Fundamentals of the Eclipse Modeling FrameworkDave Steinberg
 
Greach 2019 - Creating Micronaut Configurations
Greach 2019 - Creating Micronaut ConfigurationsGreach 2019 - Creating Micronaut Configurations
Greach 2019 - Creating Micronaut ConfigurationsIván López Martín
 
Angular JS2 Training Session #1
Angular JS2 Training Session #1Angular JS2 Training Session #1
Angular JS2 Training Session #1Paras Mendiratta
 
Porting Applications From Oracle To PostgreSQL
Porting Applications From Oracle To PostgreSQLPorting Applications From Oracle To PostgreSQL
Porting Applications From Oracle To PostgreSQLPeter Eisentraut
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsRan Mizrahi
 
OWB11gR2 - Extending ETL
OWB11gR2 - Extending ETL OWB11gR2 - Extending ETL
OWB11gR2 - Extending ETL Suraj Bang
 
Eclipse Modeling Framework
Eclipse Modeling FrameworkEclipse Modeling Framework
Eclipse Modeling FrameworkAjay K
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Jonathan Felch
 
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)Theo Jungeblut
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 

Similar a JPA - Java Persistence API (20)

Smoothing Your Java with DSLs
Smoothing Your Java with DSLsSmoothing Your Java with DSLs
Smoothing Your Java with DSLs
 
New Features of JSR 317 (JPA 2.0)
New Features of JSR 317 (JPA 2.0)New Features of JSR 317 (JPA 2.0)
New Features of JSR 317 (JPA 2.0)
 
IOC + Javascript
IOC + JavascriptIOC + Javascript
IOC + Javascript
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesChoose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
 
XML-Free Programming : Java Server and Client Development without &lt;>
XML-Free Programming : Java Server and Client Development without &lt;>XML-Free Programming : Java Server and Client Development without &lt;>
XML-Free Programming : Java Server and Client Development without &lt;>
 
Play 2.0
Play 2.0Play 2.0
Play 2.0
 
Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15Android and the Seven Dwarfs from Devox'15
Android and the Seven Dwarfs from Devox'15
 
Eclipse World 2007: Fundamentals of the Eclipse Modeling Framework
Eclipse World 2007: Fundamentals of the Eclipse Modeling FrameworkEclipse World 2007: Fundamentals of the Eclipse Modeling Framework
Eclipse World 2007: Fundamentals of the Eclipse Modeling Framework
 
CDI in JEE6
CDI in JEE6CDI in JEE6
CDI in JEE6
 
EclipseCon 2008: Fundamentals of the Eclipse Modeling Framework
EclipseCon 2008: Fundamentals of the Eclipse Modeling FrameworkEclipseCon 2008: Fundamentals of the Eclipse Modeling Framework
EclipseCon 2008: Fundamentals of the Eclipse Modeling Framework
 
Greach 2019 - Creating Micronaut Configurations
Greach 2019 - Creating Micronaut ConfigurationsGreach 2019 - Creating Micronaut Configurations
Greach 2019 - Creating Micronaut Configurations
 
Refactoring
RefactoringRefactoring
Refactoring
 
Angular JS2 Training Session #1
Angular JS2 Training Session #1Angular JS2 Training Session #1
Angular JS2 Training Session #1
 
Porting Applications From Oracle To PostgreSQL
Porting Applications From Oracle To PostgreSQLPorting Applications From Oracle To PostgreSQL
Porting Applications From Oracle To PostgreSQL
 
How AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design PatternsHow AngularJS Embraced Traditional Design Patterns
How AngularJS Embraced Traditional Design Patterns
 
OWB11gR2 - Extending ETL
OWB11gR2 - Extending ETL OWB11gR2 - Extending ETL
OWB11gR2 - Extending ETL
 
Eclipse Modeling Framework
Eclipse Modeling FrameworkEclipse Modeling Framework
Eclipse Modeling Framework
 
Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)Groovy On Trading Desk (2010)
Groovy On Trading Desk (2010)
 
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
Lego for Software Engineers at Silicon Valley Code Camp 2011 (2010-10-10)
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 

Más de Thomas Wöhlke

Stahlkirche Otto Bartning 1928
Stahlkirche  Otto Bartning 1928Stahlkirche  Otto Bartning 1928
Stahlkirche Otto Bartning 1928Thomas Wöhlke
 
Pierre Bourdieu - Elemente einer soziologischen Theorie der Kunstwahrnehmung
Pierre Bourdieu - Elemente einer soziologischen Theorie der KunstwahrnehmungPierre Bourdieu - Elemente einer soziologischen Theorie der Kunstwahrnehmung
Pierre Bourdieu - Elemente einer soziologischen Theorie der KunstwahrnehmungThomas Wöhlke
 
Treesort Algorithmus und Datenstruktur
Treesort Algorithmus und DatenstrukturTreesort Algorithmus und Datenstruktur
Treesort Algorithmus und DatenstrukturThomas Wöhlke
 
Produkt und Unternehmens Vorstellung in der Berufsschule
Produkt und Unternehmens Vorstellung in der BerufsschuleProdukt und Unternehmens Vorstellung in der Berufsschule
Produkt und Unternehmens Vorstellung in der BerufsschuleThomas Wöhlke
 
OO Methodik für eCommerce-Engineering und -Consulting mit UML
OO Methodik für eCommerce-Engineering und -Consulting mit UMLOO Methodik für eCommerce-Engineering und -Consulting mit UML
OO Methodik für eCommerce-Engineering und -Consulting mit UMLThomas Wöhlke
 
Zeitmanagement - Referat
Zeitmanagement - ReferatZeitmanagement - Referat
Zeitmanagement - ReferatThomas Wöhlke
 

Más de Thomas Wöhlke (7)

Stahlkirche Otto Bartning 1928
Stahlkirche  Otto Bartning 1928Stahlkirche  Otto Bartning 1928
Stahlkirche Otto Bartning 1928
 
Pierre Bourdieu - Elemente einer soziologischen Theorie der Kunstwahrnehmung
Pierre Bourdieu - Elemente einer soziologischen Theorie der KunstwahrnehmungPierre Bourdieu - Elemente einer soziologischen Theorie der Kunstwahrnehmung
Pierre Bourdieu - Elemente einer soziologischen Theorie der Kunstwahrnehmung
 
Treesort Algorithmus und Datenstruktur
Treesort Algorithmus und DatenstrukturTreesort Algorithmus und Datenstruktur
Treesort Algorithmus und Datenstruktur
 
Produkt und Unternehmens Vorstellung in der Berufsschule
Produkt und Unternehmens Vorstellung in der BerufsschuleProdukt und Unternehmens Vorstellung in der Berufsschule
Produkt und Unternehmens Vorstellung in der Berufsschule
 
OO Methodik für eCommerce-Engineering und -Consulting mit UML
OO Methodik für eCommerce-Engineering und -Consulting mit UMLOO Methodik für eCommerce-Engineering und -Consulting mit UML
OO Methodik für eCommerce-Engineering und -Consulting mit UML
 
Zeitmanagement - Referat
Zeitmanagement - ReferatZeitmanagement - Referat
Zeitmanagement - Referat
 
Max Imdahl Ikonik
Max Imdahl IkonikMax Imdahl Ikonik
Max Imdahl Ikonik
 

Último

BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceDelhi Call girls
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxraffaeleoman
 
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Delhi Call girls
 
Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Chameera Dedduwage
 
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara ServicesVVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara ServicesPooja Nehwal
 
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Hasting Chen
 
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024eCommerce Institute
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...Sheetaleventcompany
 
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxNikitaBankoti2
 
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptxMohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptxmohammadalnahdi22
 
Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Vipesco
 
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, YardstickSaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, Yardsticksaastr
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubssamaasim06
 
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night EnjoyCall Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night EnjoyPooja Nehwal
 
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )Pooja Nehwal
 
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Kayode Fayemi
 
Mathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMoumonDas2
 
Microsoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AIMicrosoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AITatiana Gurgel
 
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Pooja Nehwal
 
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...henrik385807
 

Último (20)

BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 93 Noida Escorts >༒8448380779 Escort Service
 
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptxChiulli_Aurora_Oman_Raffaele_Beowulf.pptx
Chiulli_Aurora_Oman_Raffaele_Beowulf.pptx
 
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
Night 7k Call Girls Noida Sector 128 Call Me: 8448380779
 
Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)Introduction to Prompt Engineering (Focusing on ChatGPT)
Introduction to Prompt Engineering (Focusing on ChatGPT)
 
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara ServicesVVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
VVIP Call Girls Nalasopara : 9892124323, Call Girls in Nalasopara Services
 
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
Re-membering the Bard: Revisiting The Compleat Wrks of Wllm Shkspr (Abridged)...
 
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
Andrés Ramírez Gossler, Facundo Schinnea - eCommerce Day Chile 2024
 
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
No Advance 8868886958 Chandigarh Call Girls , Indian Call Girls For Full Nigh...
 
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docxANCHORING SCRIPT FOR A CULTURAL EVENT.docx
ANCHORING SCRIPT FOR A CULTURAL EVENT.docx
 
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptxMohammad_Alnahdi_Oral_Presentation_Assignment.pptx
Mohammad_Alnahdi_Oral_Presentation_Assignment.pptx
 
Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510Thirunelveli call girls Tamil escorts 7877702510
Thirunelveli call girls Tamil escorts 7877702510
 
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, YardstickSaaStr Workshop Wednesday w/ Lucas Price, Yardstick
SaaStr Workshop Wednesday w/ Lucas Price, Yardstick
 
Presentation on Engagement in Book Clubs
Presentation on Engagement in Book ClubsPresentation on Engagement in Book Clubs
Presentation on Engagement in Book Clubs
 
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night EnjoyCall Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
Call Girl Number in Khar Mumbai📲 9892124323 💞 Full Night Enjoy
 
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
WhatsApp 📞 9892124323 ✅Call Girls In Juhu ( Mumbai )
 
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
Governance and Nation-Building in Nigeria: Some Reflections on Options for Po...
 
Mathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptxMathematics of Finance Presentation.pptx
Mathematics of Finance Presentation.pptx
 
Microsoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AIMicrosoft Copilot AI for Everyone - created by AI
Microsoft Copilot AI for Everyone - created by AI
 
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
Navi Mumbai Call Girls Service Pooja 9892124323 Real Russian Girls Looking Mo...
 
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
CTAC 2024 Valencia - Sven Zoelle - Most Crucial Invest to Digitalisation_slid...
 

JPA - Java Persistence API

  • 1. JPA - Java Persistence API Thomas Wöhlke ObjectCode GmbH 12.03.2009
  • 2. JPA: Agenda © 2009 ObjectCode GmbH
  • 3. Domain Object Model © 2009 ObjectCode GmbH
  • 4. Object-Relational Mapping Analogie: OO  RDB  Klasse  Tabelle  Objekt  Zeile  Variable Spalte  Wert  Feld Domain Object Modell = ERD ? © 2009 ObjectCode GmbH
  • 5. O/R Impedance Mismatch © 2009 ObjectCode GmbH
  • 6. O/R Impedance Mismatch © 2009 ObjectCode GmbH
  • 7. Domain Object Model: GLE © 2009 ObjectCode GmbH
  • 8. ... und die Physik? • v(Harddisk) << v(RAM) • CPU 99% idle • Process 99% IO_WAIT • Page Impressions  SQL-Requests? © 2009 ObjectCode GmbH
  • 9. Anno Domini 2004... © 2009 ObjectCode GmbH © 2004-2005 TheServerside.com
  • 10. Hibernate Mapping von POJO‘s: 2. Java Bean API 3. Collection API (Generics) 4. Mapping: XML oder Hibernate-Annotations Hibernate ist ein JPA-Vendor:  Hibernate-Core  Hibernate-Annotations  Hibernate Entity Manager © 2009 ObjectCode GmbH
  • 11. Von Hibernate nach JPA © 2009 ObjectCode GmbH
  • 12. JPA im JEE-Stack © 2009 ObjectCode GmbH
  • 13. persistence.xml (Java EE) <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="JPM_DB"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/JpmDS</jta-data-source> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.show_sql" value="true"/> <!-- These are the default for JBoss EJB3, but not for HEM: --> <property name="hibernate.cache.provider_class" value="org.hibernate.cache.HashtableCacheProvider"/> <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/> </properties> </persistence-unit> </persistence> © 2009 ObjectCode GmbH
  • 14. persistence.xml (Java SE) <?xml version="1.0" encoding="UTF-8"?> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="JPM_DB"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>org.woehlke.projecteering.kernel.calendar.pao.Day</class> <class>org.woehlke.projecteering.kernel.minutes.pao.Event</class> <class>org.woehlke.projecteering.kernel.minutes.pao.Minutes</class> <class>org.woehlke.projecteering.kernel.minutes.pao.MinutesItem</class> <class>org.woehlke.projecteering.kernel.projects.pao.Project</class> <class>org.woehlke.projecteering.kernel.projects.pao.ProjectCategory</class> <class>org.woehlke.projecteering.kernel.timerecording.pao.TimeRecordingItem</class> <class>org.woehlke.projecteering.kernel.userrights.pao.Company</class> <class>org.woehlke.projecteering.kernel.userrights.pao.Team</class> <class>org.woehlke.projecteering.kernel.userrights.pao.User</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.generate_statistics" value="true"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/> <property name="hibernate.connection.username" value="jpm"/> <property name="hibernate.connection.password" value="jpmpwd"/> <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/jpm"/> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> </properties> </persistence-unit> </persistence> © 2009 ObjectCode GmbH
  • 15. Mapping der Klassen 1 public class Customer extends Person { @OneToMany(mappedBy=“purchaser”) Set<Order> orders = new HashSet<Order>(); protected Customer() { } // for loading from db public Customer(String fname, String lname) { super(fname, lname); } public void addOrder(Order o) { orders.add(o); } public Set<Order> getOrders() { return orders; } } © 2009 ObjectCode GmbH
  • 16. Mapping der Klassen 2 @Entity @Table(name=“PRODUCTS”) public class Product { @Id @GeneratedValue @Column(name=“PRODUCT_PK”) long id; @Version int oplock; // column defaults to “OPLOCK” String name; // column defaults to “NAME” @ManyToOne @JoinColumn(name=“SUPP_FK”, referencedColumnName=“SUPP_PK”) Supplier supplier; ... } © 2009 ObjectCode GmbH
  • 17. Mapping der Assoziationen Kardinalität:  1:1  OneToOne  1:n  OneToMany  n:m  ManyToMany Richtung:  1:n -> OneToMany  N:1 <- ManyToOne Sichtbarkeit:  Unidirektional ->  Bidirektional <-> © 2009 ObjectCode GmbH
  • 18. Mapping der Vererbung 2. Eine Tabelle pro Klassen-Hierarchie 3. Eine Tabelle pro konkrete Klasse 4. Eine Tabelle pro Subklasse 5. Non-Entity Vererbung 6. Keine Vererbung: Embbeding © 2009 ObjectCode GmbH
  • 19. Mapping der Vererbung? © 2009 ObjectCode GmbH
  • 20. Einsatz von JPA im JBoss/EJB3 @Stateless public class MinutesItemDao extends BaseDao<MinutesItem> implements IMinutesItemDao { @PersistenceContext(unitName = "JPM_DB") private EntityManager entityManager; public MinutesItem findById(Long id) { return entityManager.find(MinutesItem.class,id); } public EntityManager getEntityManager() { return entityManager; } public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } } © 2009 ObjectCode GmbH
  • 21. Einsatz von JPA in Spring/Tomcat @Transactional public class MinutesDao extends BaseDao<Minutes> implements IMinutesDao { public Minutes findById(Long id) { return jpaTemplate.find(Minutes.class,id); } } © 2009 ObjectCode GmbH
  • 22. EJB-QL Query q = em.createQuery(“select c from Customer c where c.firstName = :fname order by c.lastName”); q.setParameter(“fname”, “Joe”); q.setFirstResult(20); q.setMaxResults(10); List<Customer> customers = (List<Customer>) q.getResultList(); // all orders, as a named query @Entity @NamedQuery(name=“Order:findAllOrders”, query=“select o from Order o”); public class Order { ... } Query q = em.createNamedQuery(“Order:findAllOrders”); © 2009 ObjectCode GmbH
  • 23. Lebenszyklus Persistente Objekte 2. Neu, transient (@Id id == null) 3. Persistent (@Id id != null) 4. Detached: – Wie persistent (@Id id!= null) – Jedoch ausserhalb des EntityManager Kontext – Lazy Loading nicht möglich! – Änderungen in DB sichern mit merge © 2009 ObjectCode GmbH
  • 24. EJB-QL // all people, via a custom SQL statement Query q = em.createNativeQuery(“SELECT ID, VERSION, SUBCLASS, FIRSTNAME, LASTNAME FROM PERSON”, Person.class); List<Person> people = (List<Person>) q.getResultList(); // single-result aggregate: average order total price Query q = em.createQuery(“select avg(i.price) from Item i”); Number avgPrice = (Number) q.getSingleResult(); // traverse to-many relations Query q = em.createQuery(“select o from Order o left join o.items li where li.price > :price”); q.setParameter(“price”, 1000); List<Order> orders = (List<Order>) q.getResultList(); © 2009 ObjectCode GmbH
  • 25. Lazy Loading • Supplier s = order.getItem().getProduct().getSupplier(); • Bei Aufruf eines Getters wird Objekt aus DB-Zeile nachgeladen. • Ohne Lazy Loading muss komplettes Objekt-Netz geladen werden. • Struktur des Objekt-Netzes variiert je nach Web-View © 2009 ObjectCode GmbH
  • 26. DAO und „Unit of Work“ © 2009 ObjectCode GmbH
  • 27. Ausblick: Seam • Kern-Entwickler von Hibernate sind nun im Seam-Projekt • O/R-Mapping von EJB3/JPA auch für die Webapplikation • OO im Datenbankbackend durch ORM • OO im Webfrontend durch JSF • Im Conversation-Scope ist Lazy-Loading möglich. • Detached Objects können für die Webview verwendet werden: Kein DTO-Antipattern © 2009 ObjectCode GmbH
  • 28. RTFM: http://www.hibernate.org/ O‘Reilly: Enterprise JavaBeans 3.0 Manning: EJB3 in Action Manning: Hibernate in Action Literatur © 2009 ObjectCode GmbH
  • 29. FRAGEN? Fragen! ... Vielen Dank für die Aufmerksamkeit © 2009 ObjectCode GmbH