SlideShare ist ein Scribd-Unternehmen logo
1 von 21
PERFORMANTE 
JAVA ENTERPRISE APPLIKATIONEN 
TROTZ O/R-MAPPING 
Simon Martinelli, simas GmbH 
@simas_ch | about.me/simas_ch 
https://github.com/simasch/orders
DAS PROBLEM
DAS MODELL
N+1 SELECT PROBLEM 
• Orders und OrderItems = FetchType.LAZY 
@OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = 
true) 
private Set<Order> orders; 
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) 
private List<OrderItem> items; 
• Ein Query für alle Customers 
• Pro Customer 1 Query für die Orders 
• Pro Order 1 Query für die OrderItems
LÖSUNGSANSÄTZE 
• FetchType.EAGER oder EntityGraph (JPA 2.1) 
– Nur ein Hinweis für JPA 
– != SQL JOIN 
– Hibernate erlaubt nur eine List mit 
FetchType.EAGER pro Entity 
org.hibernate.HibernateException: cannot simultaneously fetch 
multiple bags 
• JOIN FETCH 
– Achtung vor kartesischen Produkten
DTO 
Quelle: Martin Fowler, http://martinfowler.com/eaaCatalog/dataTransferObject.html
CustomerInfoDTO 
public class CustomerInfoDTO { 
private final Long id; 
private final String lastname; 
private final String firstname; 
private final double revenue; 
public CustomerInfoDTO(Long id, String lastname, String firstname, 
double revenue) { 
this.id = id; 
this.lastname = lastname; 
this.firstname = firstname; 
this.revenue = revenue; 
} 
...
CONSTRUCTOR EXPRESSION 
Query q = em.createQuery( 
"SELECT " + 
"NEW control.CustomerInfoDTO(c.id, c.lastname, c.firstname," + 
"SUM(i.product.price)) " + 
"FROM Customer c " + 
"JOIN c.orders o " + 
"JOIN o.items i " + 
"GROUP BY c.lastnamem, c.firstname" + 
"ORDER BY c.lastname, c.firstname"); 
List<CustomerInfoDTO> list = q.getResultList();
DAS DATENMODELL IM ZENTRUM 
• Ein 1-1 Abbild der Datenbank in Entities oft 
unnötig 
X
WAS IST MIT JPQL OHNE DIE 
BEZIEHUNG CUSTOMER->ORDER? 
NEU IN JPA 2.1 
Query q = em.createQuery( 
"SELECT " + 
"NEW control.CustomerInfoDTO(c.id, c.lastname, c.firstname, " + 
"SUM(i.product.price)) " + 
"FROM Customer c " + 
"JOIN c.orders o ON o.customerId = c.id " + 
"JOIN o.items i " + 
"GROUP BY c.lastname, c.firstname" + 
"ORDER BY c.lastname, c.firstname"); 
List<CustomerInfoDTO> list = q.getResultList();
ORM FÜR ALLES? 
Just because you're using Hibernate, doesn't 
mean you have to use it for everything. 
A point I've been making for about ten years 
now. 
Gavin King, 10.12.2013
SQL? 
• VORTEILE 
– Testbar mit SQL Developer/TOAD/IDE 
– Optimierbar (Execution Plan) 
• ABER SQL VON HAND? NEIN! 
– JPA 2.1 ConstructorResult 
– QLRM 
– jOOQ
JPA 2.1 CONSTRUCTOR RESULT 
Query q = em.createNativeQuery( 
"SELECT C.ID, C.LASTNAME, C.FIRSTNAME, SUM(P.PRICE) AS REVENUE" + 
"FROM CUSTOMERS C " + 
"JOIN ORDERS O ON O.CUSTOMER_ID = C.ID " + 
"JOIN ORDERITEMS I ON I.ORDER_ID = O.ID " + 
"JOIN PRODUCTS P ON P.ID = I.PRODUCT_ID " + 
"GROUP BY C.ID, C.LASTNAME, C.FIRSTNAME " + 
"ORDER BY C.LASTNAME, C.FIRSTNAME", "CustomerInfoDTO"); 
@SqlResultSetMapping(name="CustomerInfoDTO", 
classes={ 
@ConstructorResult(targetClass=CustomerInfoDTO.class, 
columns={@ColumnResult(name="ID"), 
@ColumnResult(name="LASTNAME"), 
@ColumnResult(name="FIRSTNAME"), 
@ColumnResult(name="REVENUE", type=Double.class)}) 
})
QLRM 
Query q = em.createNativeQuery( 
"SELECT C.ID, C.LASTNAME, C.FIRSTNAME, SUM(P.PRICE) AS REVENUE" + 
"FROM CUSTOMERS C " + 
"JOIN ORDERS O ON O.CUSTOMER_ID = C.ID " + 
"JOIN ORDERITEMS I ON I.ORDER_ID = O.ID " + 
"JOIN PRODUCTS P ON P.ID = I.PRODUCT_ID " + 
"GROUP BY C.ID, C.LASTNAME, C.FIRSTNAME " + 
"ORDER BY C.LASTNAME, C.FIRSTNAME"); 
JpaResultMapper mapper = new JpaResultMapper(); 
List<CustomerInfoDTO> list = 
jpaResultMapper.list(q, CustomerInfoDTO.class); 
QLRM: https://github.com/simasch/qlrm
jOOQ 
List<CustomersInfoDTO> list = create. 
select(CUSTOMERS.ID, CUSTOMERS.LASTNAME, CUSTOMERS.FIRSTNAME, 
sum(PRODUCTS.PRICE)). 
from(CUSTOMERS). 
join(ORDERS).on(ORDERS.CUSTOMER_ID.eq(CUSTOMERS.ID)). 
join(ORDERITEMS).on(ORDERITEMS.ORDER_ID.eq(ORDERS.ID)). 
join(PRODUCTS).on(PRODUCTS.ID.eq(ORDERITEMS.PRODUCT_ID)). 
groupBy(CUSTOMERS.ID, CUSTOMERS.NAME). 
orderBy(CUSTOMERS.NAME). 
fetchInto(CustomersInfoDTO.class); 
jOOQ: http://www.jooq.org
CQRS 
Quelle: Martin Fowler, http://martinfowler.com/bliki/CQRS.html
CQRS IM KLEINEN 
• QUERIES 
– JPA Constructor Expression 
– JPA ConstructorResult 
– QLRM 
– jOOQ 
• COMMANDS 
– JPA Entities
EMPFEHLUNGEN 
• Entities zum Erstellen und Ändern der Daten 
• DTO für lesende Zugriffe 
– Constructor Expression 
– SQL mit QLRM oder jOOQ
BONUS MATERIAL
RANDOM-DATA-GENERATOR 
RandomDataGenerator randomDataGenerator = new RandomDataGenerator(); 
List<Customer> customers = randomDataGenerator.generateList( 
400, 
new GenConfig() 
.name(Name.Firstname, "firstname") 
.name(Name.Lastname, "lastname"), 
Customer.class);
LOG4JDBC 
Anpassungen im META-INF/persistence.xml 
<property name="javax.persistence.jdbc.url" 
value="jdbc:log4jdbc:derby://localhost:1527/orders"/> 
<property name="javax.persistence.jdbc.driver" 
value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"/> 
log4jdbc-log4j2: https://code.google.com/p/log4jdbc-log4j2/

Weitere ähnliche Inhalte

Was ist angesagt?

MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0oysteing
 
C programs Set 4
C programs Set 4C programs Set 4
C programs Set 4Koshy Geoji
 
SPFx working with SharePoint data
SPFx working with SharePoint dataSPFx working with SharePoint data
SPFx working with SharePoint dataVladimir Medina
 
SPFx: Working with SharePoint Content
SPFx: Working with SharePoint ContentSPFx: Working with SharePoint Content
SPFx: Working with SharePoint ContentVladimir Medina
 
Import data from csv excel file and export to xml excel file in c programming
Import data from csv excel file and export to xml excel file in c programmingImport data from csv excel file and export to xml excel file in c programming
Import data from csv excel file and export to xml excel file in c programmingSalar Delavar Qashqai
 
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片cfc
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181Mahmoud Samir Fayed
 
Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0oysteing
 
Revision c odesagain
Revision c odesagainRevision c odesagain
Revision c odesagainrex0721
 
The Ring programming language version 1.6 book - Part 9 of 189
The Ring programming language version 1.6 book - Part 9 of 189The Ring programming language version 1.6 book - Part 9 of 189
The Ring programming language version 1.6 book - Part 9 of 189Mahmoud Samir Fayed
 
Sfdgr 12 20180906_answer_v1.0
Sfdgr 12 20180906_answer_v1.0 Sfdgr 12 20180906_answer_v1.0
Sfdgr 12 20180906_answer_v1.0 Ikou Sanuki
 
The Ring programming language version 1.7 book - Part 10 of 196
The Ring programming language version 1.7 book - Part 10 of 196The Ring programming language version 1.7 book - Part 10 of 196
The Ring programming language version 1.7 book - Part 10 of 196Mahmoud Samir Fayed
 
Aplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com BackboneAplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com BackboneRafael Felix da Silva
 
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Roel Hartman
 
Mcs011 solved assignment by divya singh
Mcs011 solved assignment by divya singhMcs011 solved assignment by divya singh
Mcs011 solved assignment by divya singhDIVYA SINGH
 
Storytelling By Numbers
Storytelling By NumbersStorytelling By Numbers
Storytelling By NumbersMichael King
 

Was ist angesagt? (20)

MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0MySQL Optimizer: What’s New in 8.0
MySQL Optimizer: What’s New in 8.0
 
Api presentation
Api presentationApi presentation
Api presentation
 
C programs Set 4
C programs Set 4C programs Set 4
C programs Set 4
 
SPFx working with SharePoint data
SPFx working with SharePoint dataSPFx working with SharePoint data
SPFx working with SharePoint data
 
SPFx: Working with SharePoint Content
SPFx: Working with SharePoint ContentSPFx: Working with SharePoint Content
SPFx: Working with SharePoint Content
 
Import data from csv excel file and export to xml excel file in c programming
Import data from csv excel file and export to xml excel file in c programmingImport data from csv excel file and export to xml excel file in c programming
Import data from csv excel file and export to xml excel file in c programming
 
Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片Contagion的Ruby/Rails投影片
Contagion的Ruby/Rails投影片
 
Ruby - Design patterns tdc2011
Ruby - Design patterns tdc2011Ruby - Design patterns tdc2011
Ruby - Design patterns tdc2011
 
The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181The Ring programming language version 1.5.2 book - Part 7 of 181
The Ring programming language version 1.5.2 book - Part 7 of 181
 
Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0Histogram Support in MySQL 8.0
Histogram Support in MySQL 8.0
 
Revision c odesagain
Revision c odesagainRevision c odesagain
Revision c odesagain
 
The Ring programming language version 1.6 book - Part 9 of 189
The Ring programming language version 1.6 book - Part 9 of 189The Ring programming language version 1.6 book - Part 9 of 189
The Ring programming language version 1.6 book - Part 9 of 189
 
Sfdgr 12 20180906_answer_v1.0
Sfdgr 12 20180906_answer_v1.0 Sfdgr 12 20180906_answer_v1.0
Sfdgr 12 20180906_answer_v1.0
 
The Ring programming language version 1.7 book - Part 10 of 196
The Ring programming language version 1.7 book - Part 10 of 196The Ring programming language version 1.7 book - Part 10 of 196
The Ring programming language version 1.7 book - Part 10 of 196
 
Aplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com BackboneAplicacoes dinamicas Rails com Backbone
Aplicacoes dinamicas Rails com Backbone
 
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...Developing A Real World Logistic Application With Oracle Application - UKOUG ...
Developing A Real World Logistic Application With Oracle Application - UKOUG ...
 
Mcs011 solved assignment by divya singh
Mcs011 solved assignment by divya singhMcs011 solved assignment by divya singh
Mcs011 solved assignment by divya singh
 
Stored procedures
Stored proceduresStored procedures
Stored procedures
 
MaintainStaffTable
MaintainStaffTableMaintainStaffTable
MaintainStaffTable
 
Storytelling By Numbers
Storytelling By NumbersStorytelling By Numbers
Storytelling By Numbers
 

Ähnlich wie Performante Java Enterprise Applikationen trotz O/R-Mapping

GraphQL - when REST API is not enough - lessons learned
GraphQL - when REST API is not enough - lessons learnedGraphQL - when REST API is not enough - lessons learned
GraphQL - when REST API is not enough - lessons learnedMarcinStachniuk
 
Spring Data for KSDG 2012/09
Spring Data for KSDG 2012/09Spring Data for KSDG 2012/09
Spring Data for KSDG 2012/09永昇 陳
 
Getting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETGetting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETTomas Jansson
 
Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Paulo Gandra de Sousa
 
PerlApp2Postgresql (2)
PerlApp2Postgresql (2)PerlApp2Postgresql (2)
PerlApp2Postgresql (2)Jerome Eteve
 
DN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project A
DN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project ADN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project A
DN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project ADataconomy Media
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateKiev ALT.NET
 
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoHasnain Iqbal
 
Agile data presentation 3 - cambridge
Agile data   presentation 3 - cambridgeAgile data   presentation 3 - cambridge
Agile data presentation 3 - cambridgeRomans Malinovskis
 
MongoDB World 2018: Keynote
MongoDB World 2018: KeynoteMongoDB World 2018: Keynote
MongoDB World 2018: KeynoteMongoDB
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31Mahmoud Samir Fayed
 
Open Source Search: An Analysis
Open Source Search: An AnalysisOpen Source Search: An Analysis
Open Source Search: An AnalysisJustin Finkelstein
 
Love Your Database Railsconf 2017
Love Your Database Railsconf 2017Love Your Database Railsconf 2017
Love Your Database Railsconf 2017gisborne
 
Advanced SQL For Data Scientists
Advanced SQL For Data ScientistsAdvanced SQL For Data Scientists
Advanced SQL For Data ScientistsDatabricks
 
PHP cart
PHP cartPHP cart
PHP carttumetr1
 
Advanced dot net
Advanced dot netAdvanced dot net
Advanced dot netssa2010
 

Ähnlich wie Performante Java Enterprise Applikationen trotz O/R-Mapping (20)

GraphQL - when REST API is not enough - lessons learned
GraphQL - when REST API is not enough - lessons learnedGraphQL - when REST API is not enough - lessons learned
GraphQL - when REST API is not enough - lessons learned
 
Spring Data for KSDG 2012/09
Spring Data for KSDG 2012/09Spring Data for KSDG 2012/09
Spring Data for KSDG 2012/09
 
Getting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NETGetting started with Elasticsearch and .NET
Getting started with Elasticsearch and .NET
 
Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)Patterns of Enterprise Application Architecture (by example)
Patterns of Enterprise Application Architecture (by example)
 
PoEAA by Example
PoEAA by ExamplePoEAA by Example
PoEAA by Example
 
PerlApp2Postgresql (2)
PerlApp2Postgresql (2)PerlApp2Postgresql (2)
PerlApp2Postgresql (2)
 
DN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project A
DN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project ADN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project A
DN 2017 | Reducing pain in data engineering | Martin Loetzsch | Project A
 
Micro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicateMicro-ORM Introduction - Don't overcomplicate
Micro-ORM Introduction - Don't overcomplicate
 
My Sql concepts
My Sql conceptsMy Sql concepts
My Sql concepts
 
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael PizzoADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
ADO.NET Entity Framework by Jose A. Blakeley and Michael Pizzo
 
Apache spark
Apache sparkApache spark
Apache spark
 
Agile data presentation 3 - cambridge
Agile data   presentation 3 - cambridgeAgile data   presentation 3 - cambridge
Agile data presentation 3 - cambridge
 
MongoDB World 2018: Keynote
MongoDB World 2018: KeynoteMongoDB World 2018: Keynote
MongoDB World 2018: Keynote
 
The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31The Ring programming language version 1.5 book - Part 8 of 31
The Ring programming language version 1.5 book - Part 8 of 31
 
Open Source Search: An Analysis
Open Source Search: An AnalysisOpen Source Search: An Analysis
Open Source Search: An Analysis
 
Love Your Database Railsconf 2017
Love Your Database Railsconf 2017Love Your Database Railsconf 2017
Love Your Database Railsconf 2017
 
Advanced SQL For Data Scientists
Advanced SQL For Data ScientistsAdvanced SQL For Data Scientists
Advanced SQL For Data Scientists
 
PHP cart
PHP cartPHP cart
PHP cart
 
Dat402
Dat402Dat402
Dat402
 
Advanced dot net
Advanced dot netAdvanced dot net
Advanced dot net
 

Kürzlich hochgeladen

Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesŁukasz Chruściel
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfStefano Stabellini
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...OnePlan Solutions
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Natan Silnitsky
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationBradBedford3
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanyChristoph Pohl
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROmotivationalword821
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtimeandrehoraa
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odishasmiwainfosol
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf31events.com
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesPhilip Schwarz
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Angel Borroy López
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...confluent
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfMarharyta Nedzelska
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 

Kürzlich hochgeladen (20)

Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Unveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New FeaturesUnveiling the Future: Sylius 2.0 New Features
Unveiling the Future: Sylius 2.0 New Features
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Xen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdfXen Safety Embedded OSS Summit April 2024 v4.pdf
Xen Safety Embedded OSS Summit April 2024 v4.pdf
 
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
Maximizing Efficiency and Profitability with OnePlan’s Professional Service A...
 
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
Taming Distributed Systems: Key Insights from Wix's Large-Scale Experience - ...
 
How to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion ApplicationHow to submit a standout Adobe Champion Application
How to submit a standout Adobe Champion Application
 
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte GermanySuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
SuccessFactors 1H 2024 Release - Sneak-Peek by Deloitte Germany
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
How To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTROHow To Manage Restaurant Staff -BTRESTRO
How To Manage Restaurant Staff -BTRESTRO
 
SpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at RuntimeSpotFlow: Tracking Method Calls and States at Runtime
SpotFlow: Tracking Method Calls and States at Runtime
 
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company OdishaBalasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
Balasore Best It Company|| Top 10 IT Company || Balasore Software company Odisha
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Sending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdfSending Calendar Invites on SES and Calendarsnack.pdf
Sending Calendar Invites on SES and Calendarsnack.pdf
 
Folding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a seriesFolding Cheat Sheet #4 - fourth in a series
Folding Cheat Sheet #4 - fourth in a series
 
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
Alfresco TTL#157 - Troubleshooting Made Easy: Deciphering Alfresco mTLS Confi...
 
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
Catch the Wave: SAP Event-Driven and Data Streaming for the Intelligence Ente...
 
A healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdfA healthy diet for your Java application Devoxx France.pdf
A healthy diet for your Java application Devoxx France.pdf
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 

Performante Java Enterprise Applikationen trotz O/R-Mapping

  • 1. PERFORMANTE JAVA ENTERPRISE APPLIKATIONEN TROTZ O/R-MAPPING Simon Martinelli, simas GmbH @simas_ch | about.me/simas_ch https://github.com/simasch/orders
  • 4. N+1 SELECT PROBLEM • Orders und OrderItems = FetchType.LAZY @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true) private Set<Order> orders; @OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true) private List<OrderItem> items; • Ein Query für alle Customers • Pro Customer 1 Query für die Orders • Pro Order 1 Query für die OrderItems
  • 5. LÖSUNGSANSÄTZE • FetchType.EAGER oder EntityGraph (JPA 2.1) – Nur ein Hinweis für JPA – != SQL JOIN – Hibernate erlaubt nur eine List mit FetchType.EAGER pro Entity org.hibernate.HibernateException: cannot simultaneously fetch multiple bags • JOIN FETCH – Achtung vor kartesischen Produkten
  • 6. DTO Quelle: Martin Fowler, http://martinfowler.com/eaaCatalog/dataTransferObject.html
  • 7. CustomerInfoDTO public class CustomerInfoDTO { private final Long id; private final String lastname; private final String firstname; private final double revenue; public CustomerInfoDTO(Long id, String lastname, String firstname, double revenue) { this.id = id; this.lastname = lastname; this.firstname = firstname; this.revenue = revenue; } ...
  • 8. CONSTRUCTOR EXPRESSION Query q = em.createQuery( "SELECT " + "NEW control.CustomerInfoDTO(c.id, c.lastname, c.firstname," + "SUM(i.product.price)) " + "FROM Customer c " + "JOIN c.orders o " + "JOIN o.items i " + "GROUP BY c.lastnamem, c.firstname" + "ORDER BY c.lastname, c.firstname"); List<CustomerInfoDTO> list = q.getResultList();
  • 9. DAS DATENMODELL IM ZENTRUM • Ein 1-1 Abbild der Datenbank in Entities oft unnötig X
  • 10. WAS IST MIT JPQL OHNE DIE BEZIEHUNG CUSTOMER->ORDER? NEU IN JPA 2.1 Query q = em.createQuery( "SELECT " + "NEW control.CustomerInfoDTO(c.id, c.lastname, c.firstname, " + "SUM(i.product.price)) " + "FROM Customer c " + "JOIN c.orders o ON o.customerId = c.id " + "JOIN o.items i " + "GROUP BY c.lastname, c.firstname" + "ORDER BY c.lastname, c.firstname"); List<CustomerInfoDTO> list = q.getResultList();
  • 11. ORM FÜR ALLES? Just because you're using Hibernate, doesn't mean you have to use it for everything. A point I've been making for about ten years now. Gavin King, 10.12.2013
  • 12. SQL? • VORTEILE – Testbar mit SQL Developer/TOAD/IDE – Optimierbar (Execution Plan) • ABER SQL VON HAND? NEIN! – JPA 2.1 ConstructorResult – QLRM – jOOQ
  • 13. JPA 2.1 CONSTRUCTOR RESULT Query q = em.createNativeQuery( "SELECT C.ID, C.LASTNAME, C.FIRSTNAME, SUM(P.PRICE) AS REVENUE" + "FROM CUSTOMERS C " + "JOIN ORDERS O ON O.CUSTOMER_ID = C.ID " + "JOIN ORDERITEMS I ON I.ORDER_ID = O.ID " + "JOIN PRODUCTS P ON P.ID = I.PRODUCT_ID " + "GROUP BY C.ID, C.LASTNAME, C.FIRSTNAME " + "ORDER BY C.LASTNAME, C.FIRSTNAME", "CustomerInfoDTO"); @SqlResultSetMapping(name="CustomerInfoDTO", classes={ @ConstructorResult(targetClass=CustomerInfoDTO.class, columns={@ColumnResult(name="ID"), @ColumnResult(name="LASTNAME"), @ColumnResult(name="FIRSTNAME"), @ColumnResult(name="REVENUE", type=Double.class)}) })
  • 14. QLRM Query q = em.createNativeQuery( "SELECT C.ID, C.LASTNAME, C.FIRSTNAME, SUM(P.PRICE) AS REVENUE" + "FROM CUSTOMERS C " + "JOIN ORDERS O ON O.CUSTOMER_ID = C.ID " + "JOIN ORDERITEMS I ON I.ORDER_ID = O.ID " + "JOIN PRODUCTS P ON P.ID = I.PRODUCT_ID " + "GROUP BY C.ID, C.LASTNAME, C.FIRSTNAME " + "ORDER BY C.LASTNAME, C.FIRSTNAME"); JpaResultMapper mapper = new JpaResultMapper(); List<CustomerInfoDTO> list = jpaResultMapper.list(q, CustomerInfoDTO.class); QLRM: https://github.com/simasch/qlrm
  • 15. jOOQ List<CustomersInfoDTO> list = create. select(CUSTOMERS.ID, CUSTOMERS.LASTNAME, CUSTOMERS.FIRSTNAME, sum(PRODUCTS.PRICE)). from(CUSTOMERS). join(ORDERS).on(ORDERS.CUSTOMER_ID.eq(CUSTOMERS.ID)). join(ORDERITEMS).on(ORDERITEMS.ORDER_ID.eq(ORDERS.ID)). join(PRODUCTS).on(PRODUCTS.ID.eq(ORDERITEMS.PRODUCT_ID)). groupBy(CUSTOMERS.ID, CUSTOMERS.NAME). orderBy(CUSTOMERS.NAME). fetchInto(CustomersInfoDTO.class); jOOQ: http://www.jooq.org
  • 16. CQRS Quelle: Martin Fowler, http://martinfowler.com/bliki/CQRS.html
  • 17. CQRS IM KLEINEN • QUERIES – JPA Constructor Expression – JPA ConstructorResult – QLRM – jOOQ • COMMANDS – JPA Entities
  • 18. EMPFEHLUNGEN • Entities zum Erstellen und Ändern der Daten • DTO für lesende Zugriffe – Constructor Expression – SQL mit QLRM oder jOOQ
  • 20. RANDOM-DATA-GENERATOR RandomDataGenerator randomDataGenerator = new RandomDataGenerator(); List<Customer> customers = randomDataGenerator.generateList( 400, new GenConfig() .name(Name.Firstname, "firstname") .name(Name.Lastname, "lastname"), Customer.class);
  • 21. LOG4JDBC Anpassungen im META-INF/persistence.xml <property name="javax.persistence.jdbc.url" value="jdbc:log4jdbc:derby://localhost:1527/orders"/> <property name="javax.persistence.jdbc.driver" value="net.sf.log4jdbc.sql.jdbcapi.DriverSpy"/> log4jdbc-log4j2: https://code.google.com/p/log4jdbc-log4j2/