0

After moving the logic from a legacy application (SQL/coldfushion) to Spring Rest with Hibernate, we have experienced a slowness in the application. The main reason is with Hibernate we noticed many queries are generated which we used to do with one single query in the legacy application (two pages long query).

Write now, I'm looking at selecting proper fetch strategies and try to optimize code. Could you please give me any other areas that I need to investigate to optimize the Hibernate layer or any other sujjestions?

newday
  • 3,842
  • 10
  • 54
  • 79
  • To quote Gavin King, the *When you use Hibernate, you don't have to use it for everything*. Generally slowness is due to fetching too much data or lazy or eager mappings. – M. Deinum May 30 '17 at 18:47

2 Answers2

0
  • Try to use DTO not entities(you can load DTO directly from the database)
  • Review the loading strategy (Eager or Lazy)
  • Try to use Native Queries more
  • Try to use more parameters to restrict the result set
  • You also can leverage some caching technique (cache all static data)
  • Try to implement hashCode and equals for each entity
Melad Basilius
  • 3,847
  • 10
  • 44
  • 81
0
  1. If you use, HQL queries, then add the 'join fetch', It avoids the n+1 query problems. For more information on join fetch

e.g:

select a from Model a 
inner join fetch a.b as b
  1. Add 'indexes' for columns which are using in where condition.

e.g: Add index for the column 'name' which is used in where condition.

select a from Model a where a.name ='x'

Follow the below links:

http://www.thoughts-on-java.org/tips-to-boost-your-hibernate-performance/

https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html

Sudhakar
  • 3,104
  • 2
  • 27
  • 36