SlideShare a Scribd company logo
www.querity.cz
JPA & HIBERNATE
Ing. Jiří Kiml,
27/11/2014, Bern
What is missing?
www.querity.cz
Agenda
►Motivation
►JPA && Hibernate
►Where the problem is?
►Summary
►Questions
www.querity.cz
Motivation
One man’s crappy software is another man’s full
time job. ~Jessica Gaston
►I was asked to talk about it by Jakob
►I like performance analysis
►…...
www.querity.cz
Agenda
►Motivation
►JPA && Hibernate
►What is missing in JPA?
►Best practices
►Summary
►Questions
www.querity.cz
JPA && Hibernate
►JPA is the dance, Hibernate is the dancer.
►JPA is the Art, Hibernate is the artist.
►JPA is a specification to standardize ORM-APIs
►Hibernate is a vendor of a JPA implementation
“ Rules of Optimization:
Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet. ”
~ Michael A. Jackson
www.querity.cz
Agenda
►Motivation
►JPA && Hibernate
►What is missing in JPA?
►Best practices
►Summary
►Questions
www.querity.cz
What is missing in JPA 2.0?
www.querity.cz
Missing features in JPA 2.0
►@BatchSize
►@Fetch(FetchMode.SUBSELECT)
►@ForeignKey
►@Index
►Data generation
Before software can be reusable it first has to be usable.
~Ralph Johnson
www.querity.cz
Lazy or Eager fetch?
►LAZY = fetch when needed
►EAGER = fetch immediately
LAZY = fetch when TO is created!!!
www.querity.cz
@BatchSize
►Using batch fetching, Hibernate can load
several uninitialized proxies if one proxy is
accessed. Batch fetching is an
optimization of the lazy select fetching
strategy
►//FIXME HUP Find a solution for jpa 2.0
// @BatchSize(size = 100)
www.querity.cz
@Fetch(FetchMode.SUBSELECT)
►If one lazy collection or single-valued
proxy has to be fetched, Hibernate will
load all of them, re-running the original
query in a subselect.
Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.
~Mosher’s Law of Software Engineering
www.querity.cz
N+1 query problem
*----------------* *-----------------*
| pet | | owner |
|----------------| * 1 |-----------------|
| - id |-----------------| - id |
| - name | | - pet_id |
*----------------* | - name |
*-----------------*
-- get all of the pets first
select * from pet
-- get the owner for each pet returned
select * from owner where pet_id = 1
select * from owner where pet_id = 2
select * from owner where pet_id = ......
select * from owner where pet_id = N
www.querity.cz
@BatchSize(size=N)
-- get all of the pets first
select * from pet
-- get all owners in a single select
select * from owner where pet_id in (1, 2, 3, …..N)
*----------------* *-----------------*
| pet | | owner |
|----------------| * 1 |-----------------|
| - id |-----------------| - id |
| - name | | - pet_id |
*----------------* | - name |
*-----------------*
www.querity.cz
@Fetch(FetchMode.SUBSELECT)
select * from pet
select * from owner where pet_id in (select id from pet)
This is very similar to the previous examples, but all of the burden is now put on the
database; and the batch size is effectively infinity.
*----------------* *-----------------*
| pet | | owner |
|----------------| * 1 |-----------------|
| - id |-----------------| - id |
| - name | | - pet_id |
*----------------* | - name |
*-----------------*
www.querity.cz
@ForeignKey
►org.hibernate.annotations.ForeignKey
►java.persistence.ForeignKey (JPA 2.1/J2EE 7)
►Used to specify the handling of foreign key
constraints when schema generation is in
effect.
hibernate3:hbm2dd
www.querity.cz
@org.hibernate.annotations.Index
►You can define an index on a particular column
using the @Index annotation on a one column
property
►Composite index as part of @Table annotation
@org.hibernate.annotations.Table(appliesTo
= “tableName”,
indexes = { @Index(name =
“forestidx”, columnNames = { “indexedcolumn”
}) })
►Part of jpa 2.1/ J2EE 7
www.querity.cz
@UniqueConstraint
►Part of jpa 1.0
►One column: @Column(unique=true)
►Multiple columns:
@Entity
@Table(
name="EMPLOYEE",
uniqueConstraints=
@UniqueConstraint(columnNames={"EMP_ID", "EMP_NAME"})
)
public class Employee { ... }
www.querity.cz
Agenda
►Motivation
►JPA && Hibernate
►What is missing in JPA?
►Best practices
►Summary
►Questions
www.querity.cz
Best Practices
►Use JPA annotations if possible
►Use hibernate annotations if there is
no equivalent in JPA
►Do NOT use // FIXME HUP
“The question of whether computers can think is like the question
of whether submarines can swim.” ~Edsger W. Dijkstra
www.querity.cz
Agenda
►Motivation
►Swiss knife
►What do we use?
►Best practices
►Summary
►Questions
www.querity.cz
Summary – what we use?
►We can usese hibernate annotations
►We will replace (some of) them by JPA 2.1
annotations
►We will probably NEVER replace hibernate
“I’ve finally learned what ‘upward compatible’ means. It means we get to
keep all our old mistakes.” ~Dennie van Tassel
www.querity.cz
Links
http://www.jroller.com/raghukodali/entry/dto_an_antipattern_in_ejb
https://jcp.org/aboutJava/communityprocess/final/jsr317/
http://hibernate.org/
http://www.javalobby.org/java/forums/m91885142.html
www.querity.cz
Agenda
►Motivation
►Swiss knife
►What do we use?
►Best practices
►Summary
►Questions
www.querity.cz
Questions …
… and maybe answers
www.querity.cz
Thank you
Ing. Jiří Kiml,
27/11/2014, Bern

More Related Content

More from Jiří Kiml

TestNG & JPA Validation
TestNG & JPA ValidationTestNG & JPA Validation
TestNG & JPA Validation
Jiří Kiml
 
Test antipatterns
Test antipatternsTest antipatterns
Test antipatterns
Jiří Kiml
 
Design by contract
Design by contractDesign by contract
Design by contract
Jiří Kiml
 
Matlanek 2007 ids11
Matlanek 2007 ids11Matlanek 2007 ids11
Matlanek 2007 ids11
Jiří Kiml
 
Matlanek 2007 sql_optimalizace
Matlanek 2007 sql_optimalizaceMatlanek 2007 sql_optimalizace
Matlanek 2007 sql_optimalizaceJiří Kiml
 
Tdd
TddTdd

More from Jiří Kiml (6)

TestNG & JPA Validation
TestNG & JPA ValidationTestNG & JPA Validation
TestNG & JPA Validation
 
Test antipatterns
Test antipatternsTest antipatterns
Test antipatterns
 
Design by contract
Design by contractDesign by contract
Design by contract
 
Matlanek 2007 ids11
Matlanek 2007 ids11Matlanek 2007 ids11
Matlanek 2007 ids11
 
Matlanek 2007 sql_optimalizace
Matlanek 2007 sql_optimalizaceMatlanek 2007 sql_optimalizace
Matlanek 2007 sql_optimalizace
 
Tdd
TddTdd
Tdd
 

Recently uploaded

🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
campbellclarkson
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
XfilesPro
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
Yara Milbes
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio, Inc.
 
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
Luigi Fugaro
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
Patrick Weigel
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and MoreManyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
narinav14
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
ToXSL Technologies
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
Pedro J. Molina
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
kalichargn70th171
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
kgyxske
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Peter Caitens
 
DevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps ServicesDevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps Services
seospiralmantra
 
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
OnePlan Solutions
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
sjcobrien
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
Severalnines
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
Envertis Software Solutions
 

Recently uploaded (20)

🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
🏎️Tech Transformation: DevOps Insights from the Experts 👩‍💻
 
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
Everything You Need to Know About X-Sign: The eSign Functionality of XfilesPr...
 
The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024The Rising Future of CPaaS in the Middle East 2024
The Rising Future of CPaaS in the Middle East 2024
 
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data PlatformAlluxio Webinar | 10x Faster Trino Queries on Your Data Platform
Alluxio Webinar | 10x Faster Trino Queries on Your Data Platform
 
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
WMF 2024 - Unlocking the Future of Data Powering Next-Gen AI with Vector Data...
 
WWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders AustinWWDC 2024 Keynote Review: For CocoaCoders Austin
WWDC 2024 Keynote Review: For CocoaCoders Austin
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and MoreManyata Tech Park Bangalore_ Infrastructure, Facilities and More
Manyata Tech Park Bangalore_ Infrastructure, Facilities and More
 
Assure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyesAssure Contact Center Experiences for Your Customers With ThousandEyes
Assure Contact Center Experiences for Your Customers With ThousandEyes
 
How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?How Can Hiring A Mobile App Development Company Help Your Business Grow?
How Can Hiring A Mobile App Development Company Help Your Business Grow?
 
Orca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container OrchestrationOrca: Nocode Graphical Editor for Container Orchestration
Orca: Nocode Graphical Editor for Container Orchestration
 
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
A Comprehensive Guide on Implementing Real-World Mobile Testing Strategies fo...
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
 
DevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps ServicesDevOps Consulting Company | Hire DevOps Services
DevOps Consulting Company | Hire DevOps Services
 
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...Transforming Product Development using OnePlan To Boost Efficiency and Innova...
Transforming Product Development using OnePlan To Boost Efficiency and Innova...
 
Malibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed RoundMalibou Pitch Deck For Its €3M Seed Round
Malibou Pitch Deck For Its €3M Seed Round
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
 
What’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete RoadmapWhat’s New in Odoo 17 – A Complete Roadmap
What’s New in Odoo 17 – A Complete Roadmap
 

Jpa & hibernate