SlideShare a Scribd company logo
1 of 25
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 ValidationJiří Kiml
 
Test antipatterns
Test antipatternsTest antipatterns
Test antipatternsJiří Kiml
 
Design by contract
Design by contractDesign by contract
Design by contractJiří Kiml
 
Matlanek 2007 ids11
Matlanek 2007 ids11Matlanek 2007 ids11
Matlanek 2007 ids11Jiří Kiml
 
Matlanek 2007 sql_optimalizace
Matlanek 2007 sql_optimalizaceMatlanek 2007 sql_optimalizace
Matlanek 2007 sql_optimalizaceJiří Kiml
 

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 Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceanilsa9823
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 

Recently uploaded (20)

Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 

Jpa & hibernate