Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate: Discriminator based multi tenancy using filter?

I have read several articles (i.e. this and that and that) on the web regarding multi tenancy (for different clients in a SaaS application). Once you have decided to go for the discriminator based approach, the hibernate doc states, that this is not supported in version 4, but will come in version 5.

Nevertheless, you may find several articles about using hibernate filters for that purpose (i.e. this and that).

I am wondering, why there will be some special implementation for it in version 5, if a filter based solution would be fine. Thus: What are the drawbacks of the filter based solution? (I have read (see comment from 06/Dec/11) that they do not work with find statements. But no other souce seems to approve that. Is this true?)

like image 359
Chagemei Avatar asked Oct 15 '12 11:10

Chagemei


1 Answers

Some drawbacks of filter based solution:

  • When persisting, one must take care of tenant_id, it is not persisted automatically. Probably one could use a @PrePersist callback method.
  • Method entityManager.find(EntityClass.class, "ID") doesn't take filter value into account
  • Have to take care of @OneToMany relations like this:
     @OneToMany(cascade = CascadeType.ALL)
     @JoinColumn(name = "ARTICLE_ID")
     @Filter(name = "tenantFilter", condition = "tenant_id = :tenantId")
     private List<ArticleChild> children;
like image 51
dboldureanu Avatar answered Sep 22 '22 19:09

dboldureanu