I've got a domain that appears something similar to

class Foo {

  String name

  static mapping = {
     table 'foo'    

but I wish to make is a lot more like :

static mapping = {
   table "foo_${dynamicVarThatComesFromRequest}"

What I wish to know is whether or not this really is even possible?


It's possible. You can include a Hibernate interceptor to process all SQL claims and parse/replace some token within the table title you type in the mapping using the actual table title you need to use.

src/groovy/DynamicTableNameInterceptor.groovy :

import org.hibernate.EmptyInterceptor

public class DynamicTableNameInterceptor extends EmptyInterceptor {

    public String onPrepareStatement(String sql) {
         // some kind of replacement logic here
         def schema=SomeHelperClass.resolveSchema()
         return sql.replaceAll('_SCHEMA_', schema) 



beans = {
    // This is for Grails 1.3.x , in previous versions, the bean name is eventTriggeringInterceptor

I do not think that's possible. Upon application startup, the mapping closure is examined and Hibernate mapping are produced consequently. This occurs once upon startup, so dynamic resolution won't occur.

Something comparable is completed in the multi-tenant-core wordpress plugin, while using 'single tenant' setup, you've got a seperate database for every tenant.

I've found thisOrsolution fascinating, may I request the reason why you want the dynamic title? I realistically may wish to keep my Foo in a single table (either named Foo or else), but that is because I've got a difficult time thinking outdoors this area. Just idea!