3. 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
►…...
5. 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
8. 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
9. www.querity.cz
Lazy or Eager fetch?
►LAZY = fetch when needed
►EAGER = fetch immediately
LAZY = fetch when TO is created!!!
10. 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)
11. 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
12. 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
13. 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 |
*-----------------*
14. 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 |
*-----------------*
16. 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
17. 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 { ... }
19. 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
21. 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