SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais de 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
 

Mais de 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
 

Último

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxalwaysnagaraju26
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Bert Jan Schrijver
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 

Último (20)

OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
Abortion Pill Prices Boksburg [(+27832195400*)] 🏥 Women's Abortion Clinic in ...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
Abortion Pills In Pretoria ](+27832195400*)[ 🏥 Women's Abortion Clinic In Pre...
 
WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 

Jpa & hibernate