2013年2月1日 星期五

The Lost Part of Multi-tenancy in Hibernate

在Hibernate官方網站Chapter 16. Multi-tenancy中提到三種實現multi-tenancy的方法,分別是:
  1. Separate database
  2. Separate schema
  3. Partitioned (discriminator) data
前兩項在Hibernate 4已經有支援,但是最後一項discriminator很遺憾官方有說明如下:
Correlates to the partitioned (discriminator) approach. It is an error to attempt to open a session without a tenant identifier using this strategy. This strategy is not yet implemented in Hibernate as of 4.0 and 4.1. Its support is planned for 5.0.
所以在Hibernate 4之前我們得自己實現這樣的approach,深入思考一下,如果可以依照登入的使用者取得tenant ID,然後這個使用者產生的所有資料都自動填入tenant ID,而讀取資料的任何query都自動加上tenant ID這個condition,那麼不就完成了我們所想要的multi-tenancy機制嗎?所以我們將三種技術結合就可以達成這個目的:
  1. Spring Security
  2. Hibernate Filter
  3. Hibernate Interceptor
可以參考:
目前我們已經在部分系統中成功地實現這樣的概念。