Technipelago Blog Stuff that we learned...
Grails 3.2.0 - java.lang.IllegalArgumentException: Incorrect type for parameter [tenantId]
With Grails 3.2.0 native multi-tenancy has finally arrived in Grails core. This makes Grails a powerful framework for multi-tenant web applications. But these advanced features comes with a price. New kind of problems surface and you most know what you're doing more than ever.
Tested with Grails 3.2.0.RC1
Consider the following code:
Person.groovy: import grails.gorm.MultiTenant class Person implements MultiTenant{ Long tenantId String firstName String lastName def beforeValidate() { if(tenantId == null) { tenantId = Tenants.currentId() } } }
FailTestSpec.groovy: Tenants.withId(1) { new Person(firstName: "Groovy", lastName: "Grails").save(failOnError: true) }
This looks ok, doesn't it?
Well, this is what you get:
java.lang.IllegalArgumentException: Incorrect type for parameter [tenantId] at org.hibernate.internal.FilterImpl.setParameter(FilterImpl.java:82) at org.grails.orm.hibernate.AbstractHibernateDatastore.enableMultiTenancyFilter(AbstractHibernateDatastore.java:342) at org.grails.orm.hibernate.AbstractHibernateDatastore$1.call(AbstractHibernateDatastore.java:368) at groovy.lang.Closure.call(Closure.java:430) at org.grails.orm.hibernate.GrailsHibernateTemplate$1.doInHibernate(GrailsHibernateTemplate.java:140) at org.grails.orm.hibernate.GrailsHibernateTemplate.doExecute(GrailsHibernateTemplate.java:243) at org.grails.orm.hibernate.GrailsHibernateTemplate.execute(GrailsHibernateTemplate.java:187) at org.grails.orm.hibernate.GrailsHibernateTemplate.executeWithNewSession(GrailsHibernateTemplate.java:137) at org.grails.orm.hibernate.AbstractHibernateDatastore.withNewSession(AbstractHibernateDatastore.java:322) at org.grails.orm.hibernate.AbstractHibernateDatastore.withNewSession(AbstractHibernateDatastore.java:331) at grails.gorm.multitenancy.Tenants.withTenantIdInternal_closure1(Tenants.groovy:182) at grails.gorm.multitenancy.Tenants$CurrentTenant.withTenant(Tenants.groovy:273) at grails.gorm.multitenancy.Tenants.withTenantIdInternal(Tenants.groovy:181) at grails.gorm.multitenancy.Tenants.withId(Tenants.groovy:157)
Because tenantId in Person is a Long you must make sure you supply a Long parameter to withId(1).
OkTestSpec.groovy: Tenants.withId(1L) { new Person(firstName: "Groovy", lastName: "Grails").save(failOnError: true) }
« Tillbaka