As the subject implies, is there some way of doing this? Our databases are set up with one physical database which contains several logical databases, usually one for each product we develop. Some of those logical databases contain shared data which our applications all consume and/or update. The thing is, the logical databases don't have any fixed names, but the name is of course known runtime through configuration files, so we can't rely on hard-coding the schema & catalog in the annotations.
Currently we solve the issue in a somewhat hacky manner by abusing NamingStrategy#tableName(..) and appending catalog & schema there, however that does cause some issues with hibernate when it comes to inverse join columns, secondary tables and other similar annotations. For an example we have a db schema which is something like this
Physical DB - User-database - Common-database - Application specific database
Our mappings would then have @Table(name="users")
Our naming strategy would then translate the tableName to be: user-db..users (MSSQL) or user-db.users (Oracle)
I know that I can set a default schema, and default catalog, but that only helps as long as the DataSource only accesses one logical database. For some applications that's sufficient, but for the ones which access multiple logical databases we'd wind up having several SessionFactories and DataSources and lose the option of joining between the logical databases.
Ideally I'd like to have the ability to annotate something like this @Table(name="users", catalog="USER_DB_CATALOG", schema="USER_DB_SCHEMA") and on startup the mappings would query a Catalog/Schema strategy to translate to the correct values.
I've done some digging in the Hibernate code and documentation and although I can't seem to find any way of solving the issue I do hope I missed something. Closest I've gotten is TableBinder#buildAndFillTable(..) which will retrieve the schema/catalog from the mappings file if it's missing from the annotation. I'm guessing that if it were possible to do I'd find the hook somewhere around there, but sadly I've yet to find any hooks I could plug into.
So, does anyone have any suggestions or solutions for this? I saw a few similar topics both on the board, and other places, but none of them seemed to be similar enough to offer a solution to this issue.
Regard,
Alex
|