SlideShare a Scribd company logo
1 of 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/

More Related Content

What's hot

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
 

What's hot (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
 

Similar to 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
 

Similar to 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
 

Recently uploaded

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
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceBrainSell Technologies
 
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
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Andreas Granig
 
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
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprisepreethippts
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Velvetech LLC
 
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
 
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
 
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
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作qr0udbr0
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentationvaddepallysandeep122
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Matt Ray
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based projectAnoyGreter
 
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
 

Recently uploaded (20)

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
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
CRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. SalesforceCRM Contender Series: HubSpot vs. Salesforce
CRM Contender Series: HubSpot vs. Salesforce
 
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...
 
Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024Automate your Kamailio Test Calls - Kamailio World 2024
Automate your Kamailio Test Calls - Kamailio World 2024
 
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
 
Odoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 EnterpriseOdoo 14 - eLearning Module In Odoo 14 Enterprise
Odoo 14 - eLearning Module In Odoo 14 Enterprise
 
Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...Software Project Health Check: Best Practices and Techniques for Your Product...
Software Project Health Check: Best Practices and Techniques for Your Product...
 
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
 
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
 
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
 
英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作英国UN学位证,北安普顿大学毕业证书1:1制作
英国UN学位证,北安普顿大学毕业证书1:1制作
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
PREDICTING RIVER WATER QUALITY ppt presentation
PREDICTING  RIVER  WATER QUALITY  ppt presentationPREDICTING  RIVER  WATER QUALITY  ppt presentation
PREDICTING RIVER WATER QUALITY ppt presentation
 
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
Open Source Summit NA 2024: Open Source Cloud Costs - OpenCost's Impact on En...
 
MYjobs Presentation Django-based project
MYjobs Presentation Django-based projectMYjobs Presentation Django-based project
MYjobs Presentation Django-based project
 
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 - ...
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 

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/