No description, website, or topics provided.
Switch branches/tags
Nothing to show
Pull request Compare This branch is even with archanaa-karun:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Searchable - Multi Tenant Bridge (SMTB)

There are many developers who struggle to overcome the problems that exist when the two essential Grails Plugins Searchable and Multi Tenant (Core) for an application come together. Here is the solution for it.

This Plugin is to overcome the ClassCastException that occurs while the two plugins Searchable [] and Multi-Tenant Plugin (Core) [] co-exist together.

This particular exception occurs due to the dependent plugin of the Multi-Tenant Plugin (Core) called Falcone Util []. This is caused due to the modification of the session in com.infusion.util.event.spring.InterceptableSessionFactoryPostProcessor. Due to this modification, the session (SessionFactoryImpl) needed by the Indexing mechanism (Compass) is modified as the session (InterceptableSessionFactory) and hence throws the Exception at the below point of code:

public class DefaultHibernateEntityLifecycleInjector implements HibernateEntityLifecycleInjector {

		public void injectLifecycle(SessionFactory sessionFactory, HibernateGpsDevice device) throws HibernateGpsDeviceException {

    		SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) sessionFactory;

So what this plugin does is, it performs the indexing required for search by the Searchable Plugin first and then the session modification for Multi Tenant (Core) happens.

Due to this, we need to accomodate the below few drawbacks that happens with Multi Tenant Features:


Just install this Plugin and do the following modifications specific to Multi-Tenant (Core).

  1. Dynamically injected Named Parameter ":tenantId" will not work =================================================================

Earlier GORM functions were automatically injected with tenantId without explicitly specifying it. Now this will not happen.

All GORM functions backed by a Criteria object (createCriteria, countBy, exists, find, get, list, listOrderBy, withCriteria) will have to be passed with the tenantId explicitly.


Instead of ‘:tenantId’ in queries, use ‘TenantUtils.getCurrentTenant()’.


Before SMTB:

Domain.executeQuery("select d from Domain as d where d.tenantId = :tenantId")

After SMTB:

Domain.executeQuery("select d from Domain as d where d.tenantId = " + TenantUtils.getCurrentTenant())

The same holds good for executeUpdate() too.

  1. TenantId has to be artificially injected in createCriteria and withCriteria ==============================================================================

Before SMTB:

def listDomain(params) { 

        def d = Domain.createCriteria()

        def results = d.list(max: params.max, offset: params.offset)  {
            order(params.sort, params.order)

After SMTB:

def listDomain(params) { 

        def d = Domain.createCriteria()

        def results = d.list(max: params.max, offset: params.offset)  {


	        order(params.sort, params.order)
  1. All GORM function has to be artificially injected with TenantId with the help of NamedQueries ================================================================================================

For all the Domain classes which are annotated as "@MultiTenant" add namedQuery "domain":

static namedQueries = { 
  domain {
	 eq 'tenantId', TenantUtils.getCurrentTenant()

The name ‘domain’ is the name of the Named Query and can be named as anything.

Now the NamedQuery "domain" is added to each of the GORM function to retrieve the right records for the Tenant.

Before SMTB:

Domain.countByTitleAndAuthor("The Sum of All Fears", "Tom Clancy")
Domain.find(String query)

After SMTB:

Domain.domain.countByTitleAndAuthor("The Sum of All Fears", "Tom Clancy")
Domain.domain.find(String query)