Friday, September 21, 2007

Tip-tip hibernate


  1. Gunakan version yang terkini walaupun beta and banyak feature² baru menarik dan berguna.. senang cerita gunakan Hibernate 3.
  2. Buat semua associations lazy(Hibernate 3 telah setkannya secara default) dan pilih utk join atau fetch secara eager utk spesifik use-case.
  3. Definasikan strategi pengurusan session terlebih dahulu. Mungkin, kita akan menggunakan
    1. satu session per request,
    2. session per request with detached objects,
    3. session per application transaksi acros multiple request, dll.
  4. Definisikan startegi flush terlebih dahulu.
    1. Hibernate auto-flush
    2. definisikan sendiri synchronization points di mana flush ke db diperlukan
  5. Definisikan strategi caching terlebih dahulu utk
    1. data transaksi
    2. data yg dillookup tetapi berubah² utk dlm short period.. gunakan open source product spt OSCache.
  6. Cachekan entiti-entiti anda dan pastikan ia turut diaplikasikan kepada association mapping spt(set,map,bag,list)
  7. Pilih join yg betul utk entiti one-to-many, many-to-many associations, set nilai saiz batch
    utk koleksi sekiranya anda mendapatkan data secara eagerly.
  8. Gunakan optimistic concurreny melalui kolum version sekiranya boleh. Timestamp pun boleh juga. tetapi tidak dijamin. selebihnya translate lah sendiri..
  9. Understand the lifecycle of identifier creation. If you are using sequences or other database-generated identifiers, your identifiers won't be set until the new entity has been saved to the database. If you use the identifier as part of your equals() and hashCode() implementation, this means that if you add the entity to a Set before you save it, you won't be able to find it again after it's saved. I've gone to UUIDs assigned in the constructor of the entity instances for this reason, and it makes life much easier. I've heard that some people, faced with this, have made their hashCode() implementation always return the same number for every instance of an entity. While this technically fulfills the contract of hashCode() it's hardly optimal.
  10. Set your IDE up with a reference to the Hibernate source code of the distribution you are using so you can trace into it. Not only will it help you understand what's causing the behavior you're seeing, but it will help you understand how Hibernate works.
  11. Understand the 2nd level cache (and understand that the Session is your 1st level cache). Understand how it caches your entity data (as a Map of identifier to Array of field values) and collections (it just saves the identifiers and re-constitutes the objects from the entity cache).
  12. Go buy Hibernate in Action. It covers Hibernate 2.1 but the ideas are the same, there are just some new features in 3.0.
  13. Speaking of new features, look at Filters. I haven't had a chance to use them yet, but the mere fact that they make the effective date problem so easy makes them a huge win. They can also let you find the number of records in a collection, etc. without having to load the whole thing, very nice.
  14. Baseline your first working setup with memory and CPU profilers and also with a SQL profiler like IronTrack SQL, then re-run your test cases with the profilers for every configuration tweak to see what the effects are.