0

as the title says, I have an application (Spring Boot) which must execute some queries on specified objects on a SQL Server database. Such database having a single catalog and multiple schemas, which must be selected based on a previous query and some command line parameters.

I cannot seem to implement a strategy which involves Hibernate multi-tenancy, as most of the tutorials and answers on this site deal with schema names coming from a web request or an external parameter, while I need a database connection before creating the main multi-tenant EntityManager. So, I switched to a custom DataSource which tries to change the connection's default schema (using ALTER USER... WITH DEFAULT_SCHEMA = ...). But this also fails because the logged-in user does not have permission to alter his own default schema.

So I'm at a loss of what to do. Any suggestions?

1 Answer 1

3

Just create an EntityManager(Factory) per schema and put them in a map to choose from.

If you don't know the schemas before hand you can create EntityManager(Factory)s as soon as you learn about a schema.

that you can configure an EntityManagerFactory programatically.

4
  • I don't know in advance how many schemas there will be, nor what are they called. The correct schema must be retrieved from a predetermined global schema, for every query.
    – Alessandro
    Commented May 13, 2015 at 10:02
  • then create the EntityManager(Factory) as soon as you know the schema. Note, that you can configure an EntityManagerFactory programatically. Commented May 13, 2015 at 13:17
  • That I did, and it works, thanks a lot. Could you make this an answer, so I can accept it.
    – Alessandro
    Commented May 14, 2015 at 7:29
  • @Alessandro how did you fixed it with @Entity classes? As there are multiple dynamic schemas, and each entity can belong to every schema. Also if we omit schema name in @Table then it is throwing error. I am using PostgreSQL database. Commented Jun 4, 2020 at 18:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.