Hibernate vs JDBC performance
I want to show you differences beetween Hibernate and JDBC. The example which i created contain database with 2 tables: user and contract. Here you can see definition of this tables:
In my example user contain 100000 rows and contract table contain 300000 rows (each user contain 3 contracts). You can download dump of this datbase from here
I created sample project which gets all users and theirs contracts and then I can compare performance. Project you can download from here
First I tested JDBC, here you can see code which gets 300000 rows:
This test executed in 3 seconds.
When running this test you must increase max heap size.
Class User1, User2, User3, User3 looks similar, only difference is annotation @Fetch. I tested here 5 cases:
- test1 - @Fetch(FetchMode.SUBSELECT)
- test2 - @Fetch(FetchMode.SELECT)
- test3 - @Fetch(FetchMode.JOIN)
- test4 - HQL, @Fetch(FetchMode.JOIN)
- test5 - native SQL
This are results of this test:
test1 : 12
test2 : 30 (10 times slower !!!!)
test3 : 31
test4 : 12
test4 : 10
The fastest Fetch mode is subselect, but when you want to have only all users, then contracts also will be fetched, and this can be 100 times slower than JDBC.
My conclusion is that the better way to gets data from databases is using JDBC (use DAO pattern) because in bigger databases all queries must be written consciously, hibarnate is good when you have 1000 rows in table, but not milions.
Service locator for EJB