Hibernate programming practices
Until now, we saw problems with Hibernate when it is not used optimally, and how to use Hibernate to achieve better performance. The following are the best practices to follow (in terms of caching, and in general) to achieve better performance when using JPA and Hibernate.
Caching
The following are some programming tips in relation to the different caching levels in Hibernate:
- Make sure to use the same version of
hibernate-ehcache
as the version of Hibernate. - Since Hibernate caches all of the objects into the session first level cache, when running bulk queries or batch updates, it's necessary to clear the cache at certain intervals to avoid memory issues.
- When caching an entity using the second level cache, collections inside of the entity are not cached by default. In order to cache the collections, annotate the collections within the entity with
@Cacheable
and@org.hibernate.annotations.Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
. Each collection is stored...