Skip to content

Instantly share code, notes, and snippets.

@s-petit
Created November 29, 2018 15:19
Show Gist options
  • Select an option

  • Save s-petit/0c6401a7a5266c2cc23acec87e960510 to your computer and use it in GitHub Desktop.

Select an option

Save s-petit/0c6401a7a5266c2cc23acec87e960510 to your computer and use it in GitHub Desktop.
Hibernate Best Practices

The goal of this gist is to apply Hibernate best practices for performance purposes:

  • Use Bidirectional OneToMany relations, with Sets instead of Lists
  • Transactions should be inside Services in order to combine several DAO calls inside the same transaction
  • Using read-only transactions in order to describe when transactions should be readonly. Moreover, it improves performance and prevents non desired writes
  • Mark read-only entities a @immutable
  • Implement equals and hashcode with business key inside entities, which is useful especially for OneToMany relations

Interesting reads:

https://docs.jboss.org/hibernate/stable/core.old/reference/en/html/persistent-classes-equalshashcode.html https://vladmihalcea.com/hibernate-facts-equals-and-hashcode/ https://thoughts-on-java.org/best-practices-many-one-one-many-associations-mappings/ https://vladmihalcea.com/spring-read-only-transaction-hibernate-optimization/ https://vladmihalcea.com/hibernate-facts-favoring-sets-vs-bags/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment