Search code examples
hibernategrailsgrails-pluginmulti-tenant

Multitenant grails plugin - grails.plugin.multitenant.core.exception.NoCurrentTenantException


Grails giving this error when i saved an instance which is associated with @multitenant annotation but no tenant is present there. So i explicitly try to put tenant id by using

objInstance.setTenantId(tenantId)

throws this exception :

grails.plugin.multitenant.core.exception.NoCurrentTenantException: Tried to save multi-tenant domain class 'objInstance', but no tenant is set

When i use

Customer.withTenantId(tenantId){ objInstance.save(flush:true) }

then it throws this exception :

org.springframework.orm.hibernate3.HibernateSystemException: illegally attempted to associate a proxy with two open Sessions; nested exception is org.hibernate.HibernateException: illegally attempted to associate a proxy with two open Sessions

CONTROLLER CODE :

def myservice
def myAction(MOrder objInstance1){
 objInstance1.properties = params;
 objInstance1?.save(flush:true)
 myservice.callingMyserviceMEthod(objInstance1)
}

Service Code:

def callingMyserviceMEthod(MOrder objInstance1){
   objInstance1.setOrderProcess(true);
   objInstance1?.save(flush:true);

if(objInstance1.getOrderProcess()){
    // creating new object object of POrder as objInstance1
    POrder objInstance = new POrder();
    objInstance?.setName("ABC");
    objInstance?.setOrderStatus("process");
    objInstance?.setTenantId(objInstance1?.getTenantId());
    objInstance?.save(flush:true);

    // I also tried this code with Customer.withTenantId()
    /*
    Customer.withTenantId(){
        POrder objInstance = new POrder();
        objInstance?.setName("ABC");
        objInstance?.setOrderStatus("process");

        objInstance?.save(flush:true);

      } */

   }
}

Do not understand how to save that objInsatance ?????


Solution

  • use merge instead of save. objInstance?.merge();