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

KLARNA - Language Models and Knowledge Graphs: A Systems Approach
KLARNA -  Language Models and Knowledge Graphs: A Systems ApproachKLARNA -  Language Models and Knowledge Graphs: A Systems Approach
KLARNA - Language Models and Knowledge Graphs: A Systems ApproachNeo4j
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Krakówbim.edu.pl
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfkalichargn70th171
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdfStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdfsteffenkarlsson2
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Andrea Goulet
 
A Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data MigrationA Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data MigrationHelp Desk Migration
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignNeo4j
 
CompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfCompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfFurqanuddin10
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1KnowledgeSeed
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...Alluxio, Inc.
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareinfo611746
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAlluxio, Inc.
 
The Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionThe Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionWave PLM
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...naitiksharma1124
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationHelp Desk Migration
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationWave PLM
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Soroosh Khodami
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfmbmh111980
 

Recently uploaded (20)

KLARNA - Language Models and Knowledge Graphs: A Systems Approach
KLARNA -  Language Models and Knowledge Graphs: A Systems ApproachKLARNA -  Language Models and Knowledge Graphs: A Systems Approach
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdfStrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
StrimziCon 2024 - Transition to Apache Kafka on Kubernetes with Strimzi.pdf
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
A Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data MigrationA Guideline to Gorgias to to Re:amaze Data Migration
A Guideline to Gorgias to to Re:amaze Data Migration
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by Design
 
CompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdfCompTIA Security+ (Study Notes) for cs.pdf
CompTIA Security+ (Study Notes) for cs.pdf
 
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
A Python-based approach to data loading in TM1 - Using Airflow as an ETL for TM1
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
The Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion ProductionThe Impact of PLM Software on Fashion Production
The Impact of PLM Software on Fashion Production
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
A Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data MigrationA Guideline to Zendesk to Re:amaze Data Migration
A Guideline to Zendesk to Re:amaze Data Migration
 
Crafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM IntegrationCrafting the Perfect Measurement Sheet with PLM Integration
Crafting the Perfect Measurement Sheet with PLM Integration
 
Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024Secure Software Ecosystem Teqnation 2024
Secure Software Ecosystem Teqnation 2024
 
AI Hackathon.pptx
AI                        Hackathon.pptxAI                        Hackathon.pptx
AI Hackathon.pptx
 
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdfMastering Windows 7 A Comprehensive Guide for Power Users .pdf
Mastering Windows 7 A Comprehensive Guide for Power Users .pdf
 

Jpa & hibernate