das
das

Reputation: 679

Hibernate how to make schema name configurable for entity class

I have been tying to make the schema name in Entity class configurable. The code which I tried for my springboot applicqtion is given below.

    @Entity
    @Table(name="Employee", schema=Employee.schemaName)
             public class Employee implements Serializable {
        @Autowired
        private static Environment env;
        public static String schema =env.getProperty("databaseSchema");
        public static final String schemaName = schema; 
        ..........
        }

Here I was trying to get the schema name from my configuration file using Environment object and set the value to schema inside @Table annotation. But I'm getting an error at the schema name in @Table annotation that "The value for annotation attribute Table.schema must be a constant expression". And if I remove the static final from the variables schema and schemaName, then also am getting an error inside @Table that "cannot make a static reference to the non-static field Employee.schemaName". Is there any way by which I can make the schema name configurable? Thanks in advance.

Upvotes: 0

Views: 2078

Answers (1)

Jorge L. Morla
Jorge L. Morla

Reputation: 630

Maybe Hibernate multi-tenancy

see the documentation here

Multi-tenancy allows a single application instance to be served for multiple tenants on a single hosting server. This is usually performed by either Separating databases, Separating schemas or Sharing schemas.

Upvotes: 1

Related Questions