I was doing some tests on multitenancy implementations using cayenee 3.1
beta 2 and postgres , using 1 schema per tenant.

I instancend one ServerRuntime per tennant this way:

   public static ServerRuntime createServerRuntime(final String
tenantSchema) {

        ServerRuntime runtime = new ServerRuntime("/cayenne-...xml", new
Module() {

            public void configure(Binder binder) {

                binder.bind(DataMapLoader.class).toInstance(new
TenantMapLoader(tenantSchema));

            }

         // in a custom module override the schema of all loaded DataMaps
        });

        return runtime;
    }

    static class TenantMapLoader extends XMLDataMapLoader {

        private String tenantSchema;

        TenantMapLoader(String tenantSchema) {
            this.tenantSchema = tenantSchema;
        }

        public DataMap load(Resource configurationResource) {
            DataMap map = super.load(configurationResource);
            map.setDefaultSchema(tenantSchema);

            for (DbEntity e : map.getDbEntities()) {
                e.setSchema(tenantSchema);

                // workaround else cayenne would search for postgress
sequences
                // in public schema.

                String
sequence=tenantSchema.concat(".").concat(e.getPrimaryKeyGenerator().getGeneratorName());
                e.getPrimaryKeyGenerator().setGeneratorName(sequence);

            }

            return map;
        }
    }

I had to put a workaround for handling sequences in the right schema, else
cayenne would look in public schema (as performig a query in postgres
omitting the schema name will end peforming it in public schema).

Is there another way to specify to cayenne which schema must be used for
sequences?

Reply via email to